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()
});
}
Looks like this line is in error:
.eq("email", "userEmail")
It should be without quotes around the variable:
.eq("email", userEmail)
Hi Yisrael,
Thank you for taking the time out to respond. I did notice that mistake and I did change it in the code however I still have the same issue in terms of filtering. For example, I can still see the entire content from one email logged in as opposed to filtering out the data!
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.
Hi Yisrael,
I had a look at the site and now correctly call my backend function. One thing I do not get is how to make the backend function return something. So just to reiterate -
Right now, the only problem in my code is that my backend function is not returning anything but once that is successful in terms of the filtering. - it should work ?
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:
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).