Tom2015 (8) [Avatar] Offline
#1
Thank you for your exellent book!
I want to save all results of find() mongodb in array:
var dbOpen = dbHandle.open( function () {
var resultRead = readObj();
console.log('resultRead: ', resultRead);

});
var readObj = function ( obj_type, callback ) {
var arr;
if (callback)
return;
dbHandle.collection('users', function(err, collect){
collect.find({name: "Tom"}, {_id:0 }).toArray(function (err, result){
if(err){
console.log('ERROR:',err);
}
else{
console.log('result:', result);


arr = result[0];
console.log('arr: ', arr) ;
return arr;
callback( result );


}
});



});

}
----------------------------
But resultRead: undefined. How can save all results of find() mongodb in array or object? Please, help me
seasoup (37) [Avatar] Offline
#2
Re: mongodb - save all results of find() mongodb in array
It's undefined because the result is passed into the callback function, not returned. It looks like you are returning if the callback function exists. Remove the return statement and the result should be logged. You have access to the result inside the callback where the log statement is and can save it there.

These are asynchronous function calls so the results cannot be returned, they are passed into the callback function. Thanks for reading our book, glad you are enjoying it.

Josh Powell