cursor.toArray()返回一个promise,而不是array [英] cursor.toArray() returns a promise instead of array

查看:86
本文介绍了cursor.toArray()返回一个promise,而不是array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前使用节点4.3.2和mongo 2.6.我正在尝试获取整个收藏集(当前收藏集中有3个文件).当我使用这段代码时,我遇到了一个问题.

Currently using node 4.3.2 and mongo 2.6. I am attempting to get a whole collection (three documents currently in the collection). When i use this bit of code i run into an issue.

function checkUpdateTime(last_updated){
    var collection = db.collection(last_updated);
    collection.insert({a:1});
    updateTimes = collection.find({a:1}).toArray();
}
var updateTimes = [];
checkUpdateTime('last_updated');
console.log(updateTimes);

当此代码为tun时,updateTimes是一个承诺,而不是我希望的数组.目标是编辑数组,然后稍后将其插入到集合中.insert语句有效,但文档的检索根本无法达到我的预期.我已经尝试了该代码的很多版本,但没有骰子.

When this code is tun updateTimes is a promise and not the array i was hoping for. The goal is the edit the array then insert it back into the collection later.The insert statement works but the retrieval of the documents simply doesn't operate the way i was expecting. I have tried quite a few versions of this code but no dice.

我想这可以归结为我想知道为什么要兑现诺言?

I guess it boils down to me wondering why a promise is being returned?

推荐答案

MongoDB驱动程序提供了两个用于处理异步操作的选项:

The MongoDB driver offers two options for handling asynchronous operations:

  • 通过调用者传递的回调
  • 通过向呼叫者返回承诺

当您不传递回调时(如您的情况),它将返回一个Promise.

When you don't pass a callback, like in your case, it will return a promise.

所以您需要在这里做出选择.但是,您无法选择的一种选择是使此代码同步运行" .

So you need to make a choice here. One choice that you can't choose is "make this code run synchronously", though.

我更喜欢诺言:

function checkUpdateTime(last_updated){
  var collection = db.collection(last_updated);
  return collection.insert({ a : 1 }) // also async
                   .then(function() {
                     return collection.find({ a : 1 }).toArray();
                   });
}
checkUpdateTime('last_updated').then(function(updateTimes) {
  console.log(updateTimes);
});

您总是可以花更多的钱,并使用类似 Promise.coroutine ,这将使您的代码看起来更加同步(即使不是):

You could always go a bit more fancy and use something like Promise.coroutine, that will make your code look a bit more synchronous (even though it isn't):

const Promise     = require('bluebird');
const MongoClient = require('mongodb').MongoClient;

let checkUpdateTime = Promise.coroutine(function* (db, last_updated){
  let collection = db.collection(last_updated);
  yield collection.insert({ a : 1 });
  return yield collection.find({ a : 1 }).toArray();
});

Promise.coroutine(function *() {
  let db = yield MongoClient.connect('mongodb://localhost/test');
  let updateTimes = yield checkUpdateTime(db, 'foobar');
  console.log(updateTimes);
})();

async/await,使用 Babel :

const MongoClient = require('mongodb').MongoClient;

async function checkUpdateTime(db, last_updated) {
  let collection = db.collection(last_updated);
  await collection.insert({ a : 1 });
  return await collection.find({ a : 1 }).toArray();
}

(async function() {
  let db = await MongoClient.connect('mongodb://localhost/test');
  let updateTimes = await checkUpdateTime(db, 'foobar');
  console.log(updateTimes);
})();

这篇关于cursor.toArray()返回一个promise,而不是array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆