错误@数据库连接初始化:错误:数据库名称必须是字符串

时间:2018-01-05 06:40:07

标签: mongodb

我只是尝试连接mongodb。但它显示

  

错误@数据库连接初始化:错误:数据库名称   必须是一个字符串

以下是代码:

try {
mongoDb.connect("localhost:27017/sample");
var db = mongoDb
}
catch (err) {
console.log('Error @ Database Connection Initialization : ' + err);
}

1 个答案:

答案 0 :(得分:-1)

请试试这个

// Initialize connection once
MongoClient.connect("mongodb://localhost:27017/sample", function(err, database) {
  if(err) return console.error(err);

  db = database;

  // the Mongo driver recommends starting the server here because most apps *should* fail to start if they have no DB.  If yours is the exception, move the server startup elsewhere.
});