【问题标题】:nodejs mongodb bluebird error: db.getCollectionAsync is not a functionnodejs mongodb bluebird 错误:db.getCollectionAsync 不是函数
【发布时间】: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


    【解决方案1】:

    据我所知,您使用的是本机 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?

    希望这会有所帮助。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 2021-10-08
    • 1970-01-01
    • 2017-01-15
    • 2021-08-28
    • 1970-01-01
    相关资源
    最近更新 更多