nodejs mongodb bluebird错误:db.getCollectionAsync不是函数

时间:2016-11-03 21:17:06

标签: node.js mongodb promise 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);
    });
  }

我从运行在localhost上的服务器上调用它。连接工作,但后来,我收到错误: 未处理拒绝TypeError:db.getCollectionAsync不是函数

我做错了什么?是因为我在服务器端做这一切吗?如果是这样,那么后缀为Async的连接怎么办? : - (

1 个答案:

答案 0 :(得分:0)

据我所知,你使用的是原生的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?

希望这会有所帮助。

相关问题