I have two files
server.js
(function () {
var x = modules.dbFind('7');
setTimeout(function() {
console.log(x);
}, 100);
})();
and my modules.js
var dbFind = (id) => {
console.log("dbFIND function in use!");
MongoClient.connect(url, function(err, db) {
if (err) throw err;
db.collection("users").findOne({id:id}, function(err, result) {
if (err) throw err;
console.log(JSON.stringify(result));
return(result);
db.close();
});
});
};
I want to pass RESULT from modules to the server file and read it there but x = undefined. Result in modules.js - console.log(result) shows all as I wish.
I know that the problem is in reading X before result gets there - but I trying already couple days and I have no idea how to solve it.