Question

var fs = require('fs');

var mongo = require("mongodb");

var config = JSON.parse(fs.readFileSync('./config.json'));

var host = config.host;

var port = mongo.Connection.DEFAULT_PORT;

var db = new mongo.Db("Nodejs-introduction", new mongo.Server(host,port,{}));

db.open(function(error){
console.log("we are connected " + host + " : " + port );

db.collection("user",function(error,collection){
    console.log("we have the collection");

    collection.insert({
        name:"jarvis"
    }, function(err,result){
        if(err) throw err;

        console.log(result);

        db.close();
    });

});

});

For the above program. I get the following error on command prompt enter image description here

Error after changes are below :

enter image description here

Was it helpful?

Solution 2

Solved this problem by Re installing Mongodb

OTHER TIPS

Your connection is fine, I think problem may be in here

collection.insert({
    name: "jarvis"
}, function(err,result){
    console.log(err);
});

You may have problem tryin to log err while there is no error with that insert. Try making it:

collection.insert({
    name: "jarvis"
}, function(err,result){
    if(err) throw err;

    console.log(result);

    db.close();
});

And remember to close db.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top