I am trying to call a function to perform a simple database look up to return a role name from a given role key, and then use that within page code to update a users record. Ive written the database lookup as a synchronous function that returns a string value. But when, its used in the user record update, it says its returning a Promise object. What am I doing wrong please. Tese database look ups are things I will want to do often.
The function is:
export async function findRole(pRoleKey) {
let wRole;
console.log(“Find role =” + pRoleKey);
const results = await wixData.query(“role”)
.eq(“roleKey”, pRoleKey)
.find();
if (results.items.length === 0) {
return “Not found”;
} else {
wRole = results.items[0].role;
console.log("Type = " + wRole);
return wRole;
}}
and the relevant calling page code is:
wixData.query(“memberProfile”)
.eq(“_id”, wId)
.find()
.then( (results) => {
if (results.items.length > 0) {
let wRole = findRole(results.items[0].role);
console.log("MP role = " + wRole);
console.log(“Found memberProfile record”);
Many thanks for help in anticipation.
Hi Trevor,
The query has not completely executed yet in order to return the value of wRole when you get to the two console.log lines in your calling page code.
Consider creating a separate async function to call the findRole function. So, these three lines would be in that new async function where RoleFromQueryResults is the parameter passed to that function:
const wRole = await findRole(RoleFromQueryResults);
console.log("MP role = " + wRole);
console.log("Found memberProfile record");
Done this way, the above two console.log lines don’t run until the findRole function has completed.
Check out this article for an explanation of promises and async/await:
Velo: Working with Promises | Help Center | Wix.com
Anthony,
Thanks for the reply. I’m new to Wix, and also to Javascript using Promises. I’d assumed that I had implemented what you had suggested by putting the data query inside an asynchronous function that used the Await function to synchronously call the data query. I know the data query works cos I’ve output the response to the console. I’d expect that inside my findRole function, the thread would suspend until the query is complete and return the value of the query - either a string or an error message - to results. But it seems to be returning a Promise Object instead anyway. Is this a bug or am I missing something.
@allentrev88 It seems like a bug, but it’s not. Though there could be other ways to do this, calling an async function with an async function does produce the desired result. It truly does execute the query function (findRole) call before proceeding to the next lines of code (the two console.log lines). In case my description above is not clear, the code would go like this:
export async function callFindRole(RoleFromQueryResults){
const wRole = await findRole(RoleFromQueryResults);
console.log("MP role = " + wRole);
console.log("Found memberProfile record");
}
from the chunk of calling page code:
wixData.query("memberProfile")
.eq("_id", wId)
.find()
.then( (results) => {
if(results.items.length > 0) {
callFindRole(results.items[0].role);
}