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

var readObj = function ( obj_type, callback ) {
var arr;
if (callback)
dbHandle.collection('users', function(err, collect){
collect.find({name: "Tom"}, {_id:0 }).toArray(function (err, result){
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
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