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