리그캣의 개발놀이터

[MongoDB]database.collection is not a function ERROR 본문

개발 공부/Node.js(웹)

[MongoDB]database.collection is not a function ERROR

리그캣 2018. 9. 15. 15:51


다음과 같은 코드를 짜던중 (MongoDB) 연결소스


router.get('/', function(req, res, next) { MongoClient.connect(dbUrl,function(err,db){ db.collection("testCollection"); /* readData(db, function(err, data){ if(err) throw err; res.render('index', {todo:data}); }); */ db.close(); });});


해당 소스를 작성하고 실행을 하였으나


TypeError: db.collection is not a function


해당 에러가 나와서 당황스럽게 했다.


Mongo DB에서는 DB의 명을 명시해 주어야 한다.

임의로 local이라고 명시해 주었다.


router.get('/', function(req, res, next) {
MongoClient.connect(dbUrl,{ useNewUrlParser: true },function(err,database){
if(err) throw err;

var db= database.db('local'); //mongodb 3.0 이상일 경우 데이터베이스 이름 명시해야??

var collection = db.collection("testCollection");
/*
readData(db, function(err, data){
if(err) throw err;
res.render('index', {todo:data});
});
*/
database.close();
});
});


실행이 잘된다.


Comments