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

109
Views
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
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!