简体   繁体   中英

Node JS: Not inserting into mongodb

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

   });

});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM