地图/join蓝鸟和mongoose

我想映射和join我的Mongoose模型,例如:

MyModel.myPromisifiedMethod parameter, (err,res) -> .then((res) -> # do stuff to res ) .then((res) -> Promise.map res, ((eachItem) -> # do stuff to to eachItem AnotherModel.get parameter, (err,res) -> # do stuff with res and eachItem ) join eachItem, ((eachItem) -> console.log eachItem ) ) .then((finalResult) -> res.status(202).send result:finalResult ) 

地图是一个迭代器,我需要它,因为我对Mongo的请求长度是50000个文档。 但是上面的不工作,特别是连接部分。 我在网上找不到任何例子。 帮助表示赞赏。

这就是我进一步工作的方式,包含以下答案:

 MyModel.myPromisifiedMethod parameter, (err,res) -> .then((res) -> Promise.all res.map (eachItem) -> # do stuff to to eachItem AnotherModel.get parameter, (err,res) -> # do stuff with res and eachItem ) .map((res) -> # do stuff with res ) .each((res) -> # do stuff with res .then((finalResult) -> res.status(202).send result:finalResult ) 

“second” .map似乎是多余的,但它的工作原理。 .each作品都像减less一样有趣,至less我从Apache Spark知道它。 each将映射出来的和已满的承诺组合成一个数组types的对象。

但是…更改的对象不通过它只传递原始对象。 所以我将不得不实现在每个“阶段”中更改的全局对象

但是…这也行不通。 全局对象始终是eachmap之前的最后一个值。

实质上我还不知道如何得到这个工作。

我能想到的一个方法是使用Promise.all:

 function mapReduce(inputs, mapFn, reduceFn){ return Promise.all(inputs.map(mapFn)).then(reduceFn); } 

你的代码会变成这样:

 MyModel.myPromisifiedMethod parameter, (err,res) -> .then((res) -> # do stuff to res ) .then((res) -> Promise.all res.map (eachItem) -> # do stuff to to eachItem AnotherModel.get parameter, (err,res) -> # do stuff with res and eachItem ) .then((res) -> res.reduce (sumdValue, eachItem) -> # do stuff to to eachItem eachItem + sumValue ) .then((finalResult) -> res.status(202).send result:finalResult )