Before I got nuts, I have a Wix store and I would like each user to query their orders collection and be able to access content based on the results.items.length. I have tried different code but seems nothing works. Kindly help me out! Here is my code;
import wixData from ‘wix-data’ ;
wixData.query( “Stores/Orders” )
.find()
.then( (results) => {
let orders= results.items
if ( orders.length > 0 ) {
$w( ‘#group1’ ).show();
$w( “#text10” ).hide();
$w( “#button1” ).hide();
} else {
$w( ‘#group1’ ).hide();
$w( “#text10” ).show();
$w( “#button1” ).show();
$w( “#text10” ).text = “To access this page, please purchase a package.” ;
}
})
Please be sure that you paste your code as a “code snippet”. To do this, you can either click on the + on the left and then select “Add a code snippet”, which will create a box like this:
#code here
Or you can paste your code, highlight it and the click the button on the end of the context bar that says “code snippet”, which will convert the highlighted content into a code snippet format. You can still go back and update your original question and format that text as a code snippet.
To your question, first, have you tried using the built-in functionality in the member page? Wix already provides the “My Orders” page there and it is pretty good. Does it not meet your needs?
As far as your code, is the only code on the page? Have you tried inserting console.log(“got to here”) or console.log(results) type debugging messages and checked the console to see if you are getting to “here” (or whatever) or that you get any results?
For instance, I took part of your code and added it to one of my pages and added a console.log message to check it and I did see my message printed in the console. Here is what I tested:
wixData.query("Stores/Orders")
.find()
.then( (results) => {
let orders= results.items
if ( orders.length > 0) {
console.log("GOT TO HERE");
}
});
and got:
So, I obviously have orders. I can then build from there, adding my show() and hide() calls as appropriate. Since I took this code from your code above, we can say that this part is working fine.
Thanks for the reply! The code works very well on the preview, but not on the live site. I have added the console.log and I get the the success execution of the code. Where I am get it wrong?
@paiysurv Do you have data in “Stores/Orders” on the live site? Just because you have data in the sandbox doesn’t mean you have data on the live site. I thought I’d rule that out before continuing. Also, what are your console.log messages telling you on the live site?
@shannon34563 Also, non-admin cannot access to the database, even the user is the buyer.
If it is permission issue, you can see 'PREMISSION_DECLINED" on Front-end/Backend. Check your backend log too!
Yes, I can confirm there is an order that is on the live site. Here is a screenshot of the console .
Errors can stop execution. Can you either comment out the code causing those errors, fix the errors or remove the code? I’ll bet you aren’t even getting to the console.log command execution.
@shannon34563 There are actually no errors in the code and on the preview console. Kindly elaborate what you mean by console.log command execution.
All Wix Stores collections are secured as Private for Admin view only, such collections can only be communicated with via a backend file while suppressing authorization.
The query works in preview because it recognizes your Admin role while working in the editor, not so in the live site.
For the live site you need to query the collection on the backend and return the results to the frontend. Read more about Calling Backend Code From Frontend .
Basically your page code should call the backend function like
// Page Code
import {myServerFnc} from 'backend/stores';
$w.onReady(function () {
myServerFnc()
.then( (res) => {
console.log(res);
});
});
& your backend function should be something like
// Backend Filename: stores.jsw
import wixData from 'wix-data';
let options = {
"suppressAuth": true
};
export function myServerFnc() {
return wixData.query("Stores/Orders")
.find(options)
.then( (results) => {
let orders= results.items
if (orders.length > 0) {
return 'GOT HERE'; // returning value
}
});
}
3 Likes
It has worked mate! I have one concern. The query has retrieved any available data on the orders collection. How can I ensure that the data being retrieved is specific to the loggedin user?
@paiysurv try querying the " buyerInfo.id " field in the database with the user id of the logged in user .
.eq('buyerInfo.id', id)
Wow. This is a huge setback to the Store collection, both the owner and Id fields are missing from the store/orders collection. “BuyerInfo.id” is not a valid field ID, but it’s been a pleasure hunting with you.
" BuyerInfo.id " is not a valid field ID because your " B " should be in lowercase. The correct field id is " buyerInfo.id "
Query works
Thanks this was super useful, I have been trying to filter using “enteredBy.id” and for some reason that doesn’t work while buyerInfo.id works just fine.