On my Node.js platform, I have created a JavaScript function that is executed within db.eval() on MongoDB.
Here is the original JS function:
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});
});
}
This function has been minified into a string 'code' to be used with 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 necessary fields required by the function.
My main question is how can I handle the asynchronous nature of db.location.find() in order to return its result to the callback of db.eval()?
If I simply attempt to return the result from the callback of db.location.find(), nothing is returned due to it being an async call.