代码如下
var mongo = require('mongodb');
var databaseName = 'Starter',
collectionName = 'wines';
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure;
db = new Db(databaseName, server);
db.open(function(err, db) {
if(!err) {
console.log("Connected to 'winedb' database");
db.collection(collectionName, {strict:true}, function(err, collection) {
if (err) {
console.log("The 'wines' collection doesn't exist. Creating it with sample data...");
populateDB();
}
});
}
});
exports.findById = function(req, res) {
var id = req.params.id;
console.log('Retrieving wine: ' + id);
db.collection(collectionName, function(err, collection) {
collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) {
res.send(item);
});
});
};
这些代码基于带有 mongoDB 的 nodejs 的 restful api 示例。
但是,它似乎无法识别函数findOne
。有人可以指出问题吗?
错误信息:
TypeError: Cannot read property 'findOne' of undefined
最佳答案
findOne
is deprecated in the latest version of mongodb@2.x
https://github.com/mongodb/node-mongodb-native/blob/2.0/lib/collection.js
您可以改用此查询
find(query).limit(1).next(function(err, doc){
// handle data
})
关于node.js - 类型错误 : Cannot read property 'findOne' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39394582/