1

I'm having trouble getting the following to work

var data = db.collection('mycollection', function(er, collection) {
                return collection.find().toArray();
           });

The resulting data is not an array. I don't get what's wrong. How do I set a varaible to the contents of find().toArray()?

I've tried logging the contents like this so I know that there must be data:

db.collection('mycollection', function(er, collection) {
   collection.find().toArray(function(err, results) {
       for (var i = 0; i < results.length; i++) {
           console.log(results[i]);
       }
   });
});

Thanks! I'm very new to ajax programming and mongodb.

bananamuffin
  • 301
  • 1
  • 5
  • 10
  • 1
    This is basically a dupe of https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – JohnnyHK Apr 11 '17 at 18:30

1 Answers1

0

Just go it like in simple way:-

.find method always provide data in array format.

var query = db.collection.find({},function(err,data){

if(err) throw err;

console.log(data); // give in array.
})

thanks

hardy
  • 880
  • 2
  • 13
  • 30