Clearly either I don’t understand something basic about the WixDataQuery object, or I have a stupid problem in my code that I just don’t see. Can anyone help me?
I have a backend function to get the data from a collection. Very simple, the function is as follows:
import wixData from 'wix-data';
export async function getOwsCheckIns() {
try {
let queryResult = await wixData.query("OWSAttendance")
.find({ suppressAuth: true });
console.log(queryResult);
return queryResult;
} catch (error) {
console.log('customBackend.jsw > getOwsCheckIns error - details -' + error.message)
Promise.reject(
new Error('customBackend.jsw > getOwsCheckIns error - details -' + error.message));
}
}
The referenced collection exists and has 587 items, which I had imported from a CSV file. The query succeeds, which means it found my collection. However, it returns zero items in the query result.
Can someone please help me identify the stupid thing I did (or didn’t do), or what I’m missing the the Velo docs about this?
Thanks!
There is nothing wrong with your code that I can see. I tested on a site of mine as well to make sure there isn’t a bug I can recreate. Is it possible your collection data isn’t saved or a sandbox/live data sync issue?
If you. have completed some debugging steps and believe this to be a bug you can open a ticket at Contact Wix for further investigation
I published my site and ran in live mode, and it returned 50 items. But I go back to preview mode and get zero records. It appears that this function is annoyingly another Wix function that doesn’t work in preview mode, which makes it just about impossible to debug.
wix data queries do work in preview mode, so you may be experiencing a bug. I will report it in general, but if you would like someone to look specifically into your site you may create a ticket at Contact Wix
Preview mode sometimes does not show all the logs. The return should be complete but why are you using supress auth? Is it a basic collection– I would check your backend module permissions for the databases and make sure that they are not restricted. Also you can add more parameters to your query. Try running this code and see if it’ll log in the console or preview mode.
import wixData from 'wix-data';
export async function getOwsCheckIns() {
try {
let queryResult = await wixData.query("OWSAttendance")
.limit(1000)
.find({ suppressAuth: true });
const items = queryResult?.items
console.log(items, 'items returned')
if (items?.length > 0) {
return items
} else {
throw new Error('Items length is not greater than 0')
}
} catch (error) {
console.log('customBackend.jsw > getOwsCheckIns error - details -' + error.message)
Promise.reject(
new Error('customBackend.jsw > getOwsCheckIns error - details -' + error.message));
}
}
Thanks. I’m pretty sure it’s not a problem with the console log. If I call this function from a live published version of my site it works as I expect. If I run it in preview mode it doesn’t. It logs and returns an object with zero length. I’ve added more parameters to the query and the results are the same.
The reason I use suppress auth is that I’ve had the experience in the past that at least some queries don’t work without it, at least in preview mode. I only use it for functions that are called by dashboard pages, where I’m sure that only authorized users can access the page.
Yeah I get that. Usually surpress auth is needed for backend functions or different wix APIs like wix stores or wix events. They also deprecated supress auth. You can try the new version by using wixAuth.elevate() if you really need to pass the submissions. For example, if you are using wix forms you would query a form by the following:
import { submissions } from 'wix-forms.v2';
import * as wixAuth from 'wix-auth';
// just an example to see how you use it...
function tbd() {
const elevatedSubmissions = wixAuth.elevate(submissions.querySubmissionsByNamespace)
}
But for wix Data queries, I rarely have to use suppress auth and that could be the reason why you are having an issue. Try you code again without the the suppress auth in find() and make sure your collections have public access to read the data. Also set the limi to 1000 since you have ~600 items in the collection. You can change collection permission settings by following this article.
Thanks, I didn’t know suppressAuth was deprecated. I tried, what you suggested. I made sure my collection was public and remove suppressAuth from the find, but it made no difference. The query returns no results from preview mode, and returns the full data set from the live published site. At this point I’ve resigned myself to not being able to use preview mode to debug this dashboard page, which is sad because I am still learning how all this stuff works.
I discovered the following in the Wix Developer documentation:
[Testing in Preview Mode] If you interact with data from your collections, you will be using the optional sandbox (and not live) version of your collections. Changes to data that result from interactions with your site in Preview mode will persist in your optional sandbox database.
I fixed this by going to CMS from the Dashboard, selecting More Actions/Advanced Settings and turning off the Sandbox option. My data query not works in Preview mode.