【问题标题】:Promisify cursor execution: MongoDB Native DriverPromisify 游标执行:MongoDB Native Driver
【发布时间】:2014-09-12 07:05:49
【问题描述】:

我已经阅读了这些内容,但执行起来有点困难。 mongoDB promise gets returned too early

具体来说,我正在尝试对 collection.find 光标进行承诺,但我不确定我做得对...任何帮助将不胜感激:

//mongo.js

var promise = require('bluebird');
var mongodb = require('mongodb');
var mongoClient = mongodb.MongoClient;
var collection = mongodb.Collection;
promise.promisifyAll(mongodb);
promise.promisifyAll(collection.prototype);

collection.prototype._find = collection.prototype.find;
collection.prototype.find = function() {
  var cursor = this._find.apply(this, arguments);
  cursor.toArrayAsync = promise.promisify(cursor.toArray, cursor);
 return cursor;
};


var myfunction = function(callback){
  // configure and export database (async)
  mongoClient.connectAsync(database)
  .then(function(db){
    module.exports.db = db;
    promise.all([
      db.createCollectionAsync('hoth', {w:1})
        .then(function(collection){
          module.exports.hoth = collection;
          collection.ensureIndexAsync({time:-1, location:'2dsphere'})
          .done(function(index){console.log(index+' hoth');});
        }),
      db.createCollectionAsync('endor', {w:1})
        .then(function(collection){
          module.exports.endor = collection;
          collection.ensureIndexAsync({time:-1, location:'2dsphere'})
          .done(function(index){console.log(index+' endor');});
        }),
      db.createCollectionAsync('alderaan', {w:1})
        .then(function(collection){
          module.exports.alderaan = collection;
          collection.ensureIndexAsync({time:-1, location:'2dsphere'})
          .done(function(index){console.log(index+' alderaan');});
        })
    ])
    .done(function(){callback(null);});
  });
};
module.exports.myfunctionAsync = promise.promisify(myfunction);

这里是 app.js

// app.js
mongo.myfunctionAsync()
.then(function(result){
  mongo.hoth.mapReduceAsync(u.map, u.reduce,{
  out: {replace:'deathstar'}, scope:{size:0.01},
  query: {time:{$gte:ago}}, finalize:u.finalize});
}).then(function(deathstar){
  deathstar.findAsync({},{fields:{_id:0, value:1}});
}).then(function(docs){
  var final = _.map(docs, function(doc){return doc.value;});
}).then(function(final){
  mongo.alderaan.insertAsync(final, {w:1});
}).then(console.log('done'));

这是错误

Cannot call method 'findAsync' of undefined

【问题讨论】:

  • 你知道,promise 通过链接返回值(就像同步代码一样!)所以你应该return mongo.hoth....return deathstar.findAsync(...

标签: node.js mongodb promise bluebird


【解决方案1】:

使用 bluebird 2.0,您只需承诺 npm。在那个光标也被承诺之后。

这是一个带有光标的一般示例:

var MongoClient = require('bluebird').promisifyAll(require("mongodb")).MongoClient;

然后:

MongoClient.connectAsync("mongodb://localhost:27017/something")
    .then(function(db) {
        db.collection('foo').findAsync({ })
            .then(function(cursor) {       
                return cursor.toArrayAsync();
            })
           .then(function(arrayOfFoos) {
               console.log(arrayOfFoos);
           });
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 2011-10-30
    • 2013-08-05
    • 2014-10-07
    • 1970-01-01
    • 2013-11-23
    相关资源
    最近更新 更多