TypeError: Cannot read property 'then' of undefined

My frontend page is calling a function which does a DB lookup and returns a boolean value based on it’s findings. But what’s happening is that the script is running past the lookup and I’m getting undefined errors.

backend\somefile.js(w) ++++ I tried both public and backend, neither work.

export function userSubCheck(em, sub) {
wixData.query(“Members”)
.eq(“email”, em) // em = email address
.find()
.then( (results) => {
let answer = false;
let check = results[“_items”][0][sub]; // sub = boolean
if (check) {
console.log(“Is Subscribed”);
answer = true;
}
return answer;
} )
.catch( (err) => {
console.log(err);
} );
}

=======================================================
Frontend code doing the calling, I get console errors on here…

import {userSubCheck} from ‘public/userSubCheck.js’; //or backend/, neither work.

let checkCat = session.getItem(“subCheck”);
let userEmail = session.getItem(“email”);

$w.onReady(function () {

userSubCheck(userEmail,checkCat).then(status => { 
	console.log(status); 
}); 

});

=======================================================
What I see in the console log is “undefined” from the frontend, THEN I get a “Is Subscribed” from the backend/public function. Why is this happening?

Hi PJ,

Please provide more details. What is “undefined”? What messages are you getting? What messages are you getting in the backend console log?

Yisrael

Hey PJ. I think the problem may be that you’re not returning the Promise from your query. You might want to take a look at this article: Velo: Working with Promises | Help Center | Wix.com

Especially, this section: Velo: Working with Promises | Help Center | Wix.com

Thank you Sam… That one word fixed my current issue! Thank you, great info on the docs too, much appreciated. :slight_smile: