【发布时间】:2016-11-04 06:08:15
【问题描述】:
我第一次尝试使用 bluebird 以尽量减少回调和同步问题。我只是使用带有蓝鸟的本机 mongodb 客户端,如下所示:
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 为后缀的连接是如何工作的? :-(
【问题讨论】:
标签: node.js mongodb promise bluebird