Frage

I am trying to perform save a collection in mongo db using the node.js driver.I use the following code:

 require("mongodb").MongoClient.connect("mongodb://localhost:27017/course",function(db,err){
if(err) console.log(err);
else{
    var query={"assignment":"hw2"};
    db.collection("grades").findOne(query,function(err,doc){
        if(err) console.log(err);
        else{
            //console.dir(doc);
            doc["date_returned"]=new Date();
            db.collection("grades").save(doc,function(err,saved){
                if(err) console.log(err);
                else{
                    console.log("Successfully saved "+saved+" documents");
                    return db.close();
                }

            });
        }

    });
}

});

I get this huge error which I put in this pastebin

War es hilfreich?

Lösung

There is a typo in the callback of the .connect function. You exchanged the error with the result in the argument positions, so you are printing the database object instead the error. It should be:

.connect("mongodb://localhost:27017/course",function(err,db){
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top