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

108
Vistas
how to make this function syncornus (nodejs and mongodb)

i cant seem to make it work in order and it will return undefined every time so how can i make it so the console.log shows it after the function is done?

var MongoClient = require("mongodb").MongoClient;
var url = "mongodb://localhost:27017/";
let datad;
function search(dbol, collection, query, callback) {
  MongoClient.connect(url, function (err, db) {
    if (err) throw err;
    var dbo = db.db(dbol);
    dbo
      .collection(collection)
      .find(query)
      .toArray(function (err, result) {
        if (err) throw err;
        // console.log(result);
        datad = JSON.stringify(result);
        console.log(datad);
        db.close();
        return result;
      });
  });

  return datad;
}

var query = { user: "admin" };
console.log(search("storageinterface", "user", query));
console.log(datad);

the console.log inside the function gives the correct value but i cant get it out of the function in time

about 4 years ago · Juan Pablo Isaza
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