when I use "mongodb": "^3.0.5",
const cn = require('./connect');
function showItems(req, callback) {
cn.MongoClient.connect(cn.url, (err, database)=> {
const collection = database.db.collection('items');
collection.find({}).toArray((err, result)=> {
callback(result);
});
database.close();
})
}
module.exports = showItems;
I got,
/node_modules/mongodb/lib/mongo_client.js:433
throw err
^
TypeError: database.db.collection is not a function
But,when I use "mongodb": "^2.2.33" , it worked.
const cn = require('./connect');
function showItems(req, callback) {
cn.MongoClient.connect(cn.url, (err, db)=> {
const collection = db.collection('items');
collection.find({}).toArray((err, result)=> {
callback(result);
});
db.close();
})
}
module.exports = showItems;
connect.js
const url = "mongodb://127.0.0.1:27017/databasename";
const MongoClient = require('mongodb').MongoClient;
module.exports = {url, MongoClient};
Use "mongodb": "^3.0.5" , How to solve this problem ?
./connectdo ? If you want to usecn.MongoClient.connectyou shoud replaceconst cn = require('./connect');byconst cn = require('mongodb');