node.js - 连接mongodb时出现错误,Error: collection name must be a String
天蓬老师
天蓬老师 2017-04-17 15:52:43
0
1
590
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(1)
阿神

According to the documentation, Collection instances cannot be constructed directly using Collection:

Collection()
Create a new Collection instance (INTERNAL TYPE, do not instantiate directly)

The correct code is as follows:

var Db = require('mongodb').Db,
    Server = require('mongodb').Server

var db = new Db('test01', new Server('localhost', 27017));

db.open(function(err, client) {
  if (err) throw err;
  console.log('connected to server');
  var collection = db.collection('student');
  collection.find(function(err, cursor) {
    cursor.each(function(err, doc) {
      if (doc) {
        console.log(doc.uname);
      }
    });
  });
});

Refer to MongoDB official documentation

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template