0

I'm trying to insert a document in a collection and print it but for some reason all I see are undefined. I'd appreciate any help with this. I'm new to Mongo and Node so pardon me for any noob mistakes.

Thanks!

console.log(current_tree);
globaldb.collection("tree").insertOne({"tree":current_tree},function(err, result) {
    assert.equal(err, null);
    console.log("Inserted a document into the restaurants collection.");
});
globaldb.collection("tree",function(err,collection){
    collection.find({},function(err, companies) {
        companies.forEach(function(err,company){
                console.log(company);
        });
    });
});

Console Logs

gatech-kid
  • 67
  • 8
  • Are You using Mongoose ? Because it is like ORM tool for mongodb and very easy to use various query in mongodb. Link is http://mongoosejs.com/docs/api.html – Himanshu Goyal Apr 11 '16 at 05:58
  • i think you got some err in your foreach loop. Try printing err and see. – Vallabha Apr 11 '16 at 06:02
  • You are trying to call `.find()` "outside" of the callback where you created the data. It has not actually inserted until that callback completes. Place your `.find()` "inside" the callback instead. Also you will need [`.toArray()`](http://mongodb.github.io/node-mongodb-native/2.1/api/Cursor.html#toArray) with the native driver or otherwise process the returned stream – Neil Lunn Apr 11 '16 at 06:03

0 Answers0