똑같은 삽질은 2번 하지 말자

(Node.js)database.collection is not a function 본문

Node

(Node.js)database.collection is not a function

곽빵 2019. 10. 5. 12:31

 In mongodb version >= 3.0, That database variable is actually the parent object of the object you are trying to access with database.collection('whatever'). To access the correct object, you need to reference your database name, for me that was by doing

 

mongodb 버전 3.0이상을 사용할 때는, connection을 할 때에 database명을 명시해야 한다는 것이다.

 

function connectDB(){
    // 데이터베이스 연결정보 
    var databaseUrl = 'mongodb://localhost:27017/local';
    
    MongoClient.connect(databaseUrl, function(err,db){
        if(err) throw err;
        console.log('데이터베이스에 연결되었습니다. :' + databaseUrl );
        database = db.db('local'); 
        // database 변수에 인자로 전달된 db변수의 
        // db 함수를 이용해 local 이라는 데이터베이스 이름으로 db전달
    });
}

'Node' 카테고리의 다른 글

(Node.js)Sokcet 통신을 이용한 1:1 chat  (0) 2019.10.25
(Node.js)module.exports 와 exports 차이  (0) 2019.10.18
(Node.js) body_parser 모듈  (0) 2019.09.30
(Node.js)Express 라우터 요청(req) 응답(res) 객체  (1) 2019.09.22
Node  (0) 2019.09.19
Comments