I have written a javascript function which will get executed inside db.eval() on mongodb on my nodejs platform.
my js function is:
function(data){
var d = {
vehicle_id:data.vehicle_id,
timestamp:{
$gte:data.start_time,
$lte:data.end_time
}
};
var routeStatus = [];
db.location.find(d,function(err,result){
db.result.insert({result});
});
}
which is minified to an string 'code' to be passed to db.eval()
var code = 'function(data){var d={vehicle_id:data.vehicle_id, timestamp:{$gte:data.start_time, $lte:data.end_time}}; db.location.find(d,function(err,result){return result;});}';
db.eval(code,[info],function(err,result){
log(result);
});
the info object contains all required fields getting called by function;
Now main question is db.location.find() is an asynch call so how could i get its result return to callback of db.eval(); ?
if i simply do return result from callback of db.location.find() then i get nothing returned as its being an async call.
got the answer, thanks to @NeilLunn for giving small but useful tip. the .toArray() worked
simple doing
var docs = db.location.find().toArray();
worked for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.