5

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")
        });

   });

});
11
  • 1
    So there is a Mongo shell that comes with mongo, called mongo. What happens when you query the same data in there? Commented Aug 20, 2013 at 21:11
  • If i type mongo into terminal i get an error saying couldnt connect. Commented Aug 20, 2013 at 21:12
  • Well, in the code, you could add 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. Commented Aug 20, 2013 at 21:17
  • 1
    In the terminal it needs some parameters for the database and port. Then if it connects you can run use nodejsIntro and then db.collection.insert(stuff) or db.collection.find(stuff) where stuff is a properly formatted object for that routine. Commented Aug 20, 2013 at 21:19
  • 1
    You might consider doing some editing of your question, since you have a working reader and the CLI writer works but not the nodejs writer. This is not what you started with, so some progress was made. Maybe someone else will drop by. Commented Aug 20, 2013 at 21:43

1 Answer 1

2

re you sure you actually connected to mongo? When you connect to mongo from from the cli and type 'show dbs', do you see nodejsintro? Does the collection exist?

Also, from your code

db.open(function(err){
    //you didn't check the error
    console.log("We are connected! " + host + " : " + port);

    db.collection("user", function(error, collection){
        //here you log the error and then try to insert anyway
        console.log(error);

        collection.insert({
            id: "1",
            name: "Chris Till"
        }, function(){
                //you probably should check for an error here too
                console.log("Successfully inserted Chris Till")
        });

   });

});

If you have already adjusted the logging and are sure you are not getting any errors, let's try changing some of the connection info.

var mongo = require('mongodb');

var Server = mongo.Server,
    Db = mongo.Db,
    BSON = mongo.BSONPure;

var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('nodejsintro', server);

db.open(function(err, db) {
    if (!err) {
        console.log("Connected to 'nodejsintro' database");
        db.collection('user', {strict: true}, function(err, collection) {
            if (err) {
                console.log("The 'user' collection doesn't exist. Creating it with sample data...");
                //at this point you should call your method for inserting documents.
            }
        });
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried console logging all errors and they all return null. And when I run show dbsI can see nodejsintro 0.203125GB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.