From my code, I want the value in _status
variable after query and write file from MongoClient.connect
, but I get NULL
from return _status
. How can I pass the value outside the function? Thank you for your answer.
function query(domain)
{
var _status;
MongoClient.connect(url, function(err, db) {
if (err) throw err;
db.collection("zone").find({"name" : domain}).toArray(function(err, result) {
if (!err) {
fs.writeFile('/root/'+domain+'.txt', result, function (err) {
if (err) throw err;
});
_status = "success";
}
else {
_status = "fail";
}
});
db.close();
});
return _status;
}
https://stackoverflow.com/questions/32631790/nodejs-returning-result-on-async-result – nityanarayan44 Feb 07 '18 at 07:43