nodejs mongodb蓝鸟错误:db.getCollectionAsync不是函数
问题描述:
我想第一次使用蓝鸟来最大限度地减少回调和同步问题。我只是用本地的MongoDB客户端青鸟如下:nodejs mongodb蓝鸟错误:db.getCollectionAsync不是函数
var mongodb = require('mongodb');
var Promise = require("bluebird");
Promise.promisifyAll(mongodb);
var MongoClient = mongodb.MongoClient;
然后后来,在module.exports对象,我有:
foofunc: function(callback) {
MongoClient.connectAsync(url)
.then(function(db) {
//MongoDB should create collection if its not already there
console.log('... connect worked. OK now get bar collection');
return db.getCollectionAsync('bar');
})
.then(function(col){
//do something with the returned collection col
})
.catch(function(err){
//handle errors
console.log(err);
return callback(err);
});
}
我把这个从我的服务器上运行本地主机。连接成功,但随后的权利后,我得到的错误: 未处理的拒绝类型错误:db.getCollectionAsync不是一个函数
我在做什么错?是否因为我在服务器端执行所有操作?如果是这样,那么Async后缀的连接如何工作? :-(
答
至于我看你使用MongoDB的本地司机的NodeJS。
http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html
如果是这种情况,那么你需要使用
return db.collection('bar');
也点这里给节点说明这种方法是同步的。
这个答案也可能对你有所帮助。
How can I promisify the MongoDB native Javascript driver using bluebird?
希望这会有所帮助。
OMG !!!文档页面非常相似。我盯着https://docs.mongodb.com/v2.6/reference/method/db.getCollection/而不是http://mongodb.github.io/node-mongodb-native/2.2/api/Collection。 html !!!非常感谢现在一切正常:) – rstruck