Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

90
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda