Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

88
Views
Database count is returning Undefined

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A few issues here..

  1. Your queryDB doesn't return anything, apart from an undefined Promise
  2. Your mixing thenables with async / await, do one or the other.
  3. Your catch is doing nothing, generally speaking avoid catch, unless your trapping something specific, handle errors further up, not in util functions.

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)    
    });
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!