nodejs mongodb bluebird error: db.getCollectionAsync is not a function
nodejs mongodb bluebird error: db.getCollectionAsync is not a function
我第一次尝试使用 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 为后缀的连接如何工作? :-(
据我所知,您使用的是本机 mongodb NodeJs 驱动程序。
http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html
如果是这样的话。然后你需要使用
return db.collection('bar');
这里也指向node,这个方法是同步的
这个答案也可能对您有所帮助。
How can I promisify the MongoDB native Javascript driver using bluebird?
希望这对您有所帮助。
我第一次尝试使用 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 为后缀的连接如何工作? :-(
据我所知,您使用的是本机 mongodb NodeJs 驱动程序。
http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html
如果是这样的话。然后你需要使用
return db.collection('bar');
这里也指向node,这个方法是同步的
这个答案也可能对您有所帮助。
How can I promisify the MongoDB native Javascript driver using bluebird?
希望这对您有所帮助。