So I'm following a Node.js tutorial course on tutsplus.com which up until now has been great.
I am on the lesson regarding MongoDB and I have come a bit unstuck. I'm not sure why this doesn't work for me as it's working in the video and my code is the same. All I can think is that there has been an update since the course was made a year ago.
From trying to console.log at various points I think the data is not inserting correctly in the beginning and so nothing is returned.
Everything appears to fire as expected except the callback for cursor.toArray().
I'm currently learning node and mongodb so please bear with me if I've made an obvious mistake.
I have been instructed to write the following file and then execute it in the command line.
EDIT:
I have narrowed the problem down to the insert script. When inserting the data via the CLI it will retrieve it back.
var mongo = require('mongodb'),
host = "127.0.0.1",
port = mongo.Connection.DEFAULT_PORT,
db = new mongo.Db('nodejsintro', new mongo.Server(host, port, {}));
db.open(function(err){
console.log("We are connected! " + host + " : " + port);
db.collection("user", function(error, collection){
console.log(error);
collection.insert({
id: "1",
name: "Chris Till"
}, function(){
console.log("Successfully inserted Chris Till")
});
});
});
mongo. What happens when you query the same data in there?mongointo terminal i get an error saying couldnt connect.if (err) return console.log(err);to check that same sort of thing. Although generally it will throw errors because if there is an error the other data won't be defined.use nodejsIntroand thendb.collection.insert(stuff)ordb.collection.find(stuff)wherestuffis a properly formatted object for that routine.