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;
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");
}
});
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?
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.
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
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?
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.
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.