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();
});
});
對於上述程序。我得到以下錯誤在命令提示符下
錯誤後變化如下:
其實上市之後db.collection''你從來沒有關閉回調這個問題。那麼它實際上在哪裏關閉?該類型錯誤表明您可能引用了超出範圍的內容。 –
@NeilLunn正確安裝mongodb解決了這個問題。我認爲mongodb已經安裝,但事實證明它沒有。 –