mongoDB와 연동하면
connect 함수로 연결을 먼저 하고
var Client = require('mongodb').MongoClient;
Client.connect(url, function(err, db) {} );
함수의 인자인 db로 insert든 find든 하고싶은걸 하게 되는데,
* 예를들어,
db.collection('myCollection').insert( {title:'data'} );
컴파일 시에는 문제 없다가
db.collection() 부분에 가면 아래와 같은 에러가 났다.
db.collection is not a function
찾아보니 mongoDB를 다운그레이드 하라는 말도 있었는데
맘에드는 해결책은 아니었고
더 찾아보다가 아래와 같이 해결했다.
var Client = require('mongodb').MongoClient;
Client.connect(url, function(error, db){
if(error) {
console.log(error);
} else {
console.log("connected:"+db);
var myDB = db.db('myCollection');
myDB.collection('myCollection').insert( {title:'data'} );
// db.close();
}
});
'Node.js' 카테고리의 다른 글
[Node.js] mongoDB 전체 읽어오기 (0) | 2018.03.05 |
---|---|
MongoDB, Node.js 연동 시작하기 1 (0) | 2018.02.28 |
[node.js] view engine 여러개 설정하기 (0) | 2017.12.04 |
[node.js] 복습3. express 이용해서 프로젝트 생성하기 (0) | 2017.12.04 |
[node.js] 복습1. Node.js, NPM, Express (0) | 2017.12.04 |