I need small help in a small code. The function is returning [object Promise] and I want to return the count.
When I use console.log within the function it works perfectly fine and shows the appropriate result, however when returning the code and use console.log outside the function it is saying [object Promise].
async function queryDB(num)
{
await wixData.query("numDB")
.eq("nums", num)
.find()
.then( (results) =>
{
var count = results.items.length
console.log("count in function: " + count)
return count
})
.catch( (err) =>
{
let errorMsg = err;
} );
}
Then I call it with:
$w.onReady(function ()
{
var num1 = queryDB("1");
console.log("count out function: " + num1)
});
And I am getting the following output:
count out function: [object Promise]
count in function: 3
TIA
A few issues here..
Since your using async/await..
async function queryDB(num)
{
const results = await wixData.query("numDB")
.eq("nums", num)
.find();
var count = results.items.length
console.log("count in function: " + count)
return count;
}
$w.onReady(async function ()
{
var num1 = await queryDB("1");
console.log("count out function: " + num1)
});
For completeness, if you were not using async / await.
function queryDB(num)
{
return wixData.query("numDB") //don'f forget to return
.eq("nums", num)
.find()
.then(results => {
var count = results.items.length
console.log("count in function: " + count)
return count;
});
}
$w.onReady(function ()
{
queryDB("1").then(num1 => {
console.log("count out function: " + num1)
});
});