Hello,
I wanted to filter one of my databases through email. I was wondering where I have gone wrong if you could help me out. This is not the live 'Stores/Orders' dataset, this is one which I have added. Essentially, I am filtering through the person's logged in email and then in my front end displaying information on a repeater. Right now, everyone can see the information so it is not being filtered out properly!
Thanks
FrontEnd: (on the site page)
import {options} from 'backend/Permission.jsw';
export function orders() {
$w("#repeater2").data = orders.items;
}
BackEnd:
import wixData from 'wix-data';
import wixUsers from 'wix-users';
let options = {
"suppressAuth": true,
"suppressHooks": true
};
export function orders() {
let user = wixUsers.currentUser;
let email = user.getEmail()
user.getEmail()
.then(() => {
let userEmail = email;
wixData.query("PastOrders", options)
.eq("email", "userEmail")
.find()
});
}
In order to return data from a function you will need a return statement.
Your database query needs to handle the returned Promise. You can do that either with a .then() function, or invoking the query using await. For information on Promises, see these articles:
Corvid: Working with Promises
Promises, Promises
Also, your query needs to use the field key and not the field name. That is, you should use pastOrders and not PastOrders (field key starts with a lower case letter).
You are also not calling your backend function correctly, and the backend function is not returning anything.
See the article Corvid Web Modules: Calling Server-Side Code from the Front-End which shows how to call backend code and also gives some examples on writing backend functions.
Looks like this line is in error:
.eq("email", "userEmail")
It should be without quotes around the variable:
.eq("email", userEmail)