javascript - 使用外部 API 调用和 findOneAndUpdate 循环结果
问题描述
我正在尝试编写一个程序,该程序使用 mongoose 从 mongo 数据库获取文档并使用 API 处理它们,然后使用处理结果编辑数据库中的每个文档。我的问题是我有问题,因为我不完全理解 nodejs 和异步。这是我的代码:
Model.find(function (err, tweets) {
if (err) return err;
for (var i = 0; i < tweets.length; i++) {
console.log(tweets[i].tweet);
api.petition(tweets[i].tweet)
.then(function(res) {
TweetModel.findOneAndUpdate({_id: tweets[i]._id}, {result: res}, function (err, tweetFound) {
if (err) throw err;
console.log(tweetFound);
});
})
.catch(function(err) {
console.log(err);
})
}
})
问题是在 findOneAndUpdate 中,tweets 是未定义的,所以它找不到那个 id。有什么解决办法吗?谢谢
解决方案
您真正缺少的核心是 Mongoose API 方法也使用"Promises",但您似乎只是在使用回调从文档或旧示例中复制。解决方案是转换为仅使用 Promises。
使用 Promise
Model.find({},{ _id: 1, tweet: 1}).then(tweets =>
Promise.all(
tweets.map(({ _id, tweet }) =>
api.petition(tweet).then(result =>
TweetModel.findOneAndUpdate({ _id }, { result }, { new: true })
.then( updated => { console.log(updated); return updated })
)
)
)
)
.then( updatedDocs => {
// do something with array of updated documents
})
.catch(e => console.error(e))
除了回调的一般转换之外,主要的变化是Promise.all()
用于解决Array.map()
正在处理结果的输出,.find()
而不是for
循环。这实际上是您尝试中最大的问题之一,因为for
无法实际控制异步函数何时解析。另一个问题是“混合回调”,但这是我们通常只使用 Promises 来解决的问题。
在Array.map()
我们Promise
从 API 调用返回的内部,链接到findOneAndUpdate()
实际更新文档的 。我们还使用new: true
实际返回修改后的文档。
Promise.all()
允许“Promise 数组”解析并返回结果数组。这些你看到的updatedDocs
。这里的另一个优点是内部方法将以“并行”而不是串行方式触发。这通常意味着更快的分辨率,尽管它需要更多的资源。
另请注意,我们使用的“投影”{ _id: 1, tweet: 1 }
仅从Model.find()
结果中返回这两个字段,因为它们是其余调用中唯一使用的字段。当您不使用其他值时,这可以节省为每个结果返回整个文档。
您可以简单地Promise
从 中返回findOneAndUpdate()
,但我只是添加了 ,console.log()
因此您可以看到输出正在触发。
正常的生产使用应该没有它:
Model.find({},{ _id: 1, tweet: 1}).then(tweets =>
Promise.all(
tweets.map(({ _id, tweet }) =>
api.petition(tweet).then(result =>
TweetModel.findOneAndUpdate({ _id }, { result }, { new: true })
)
)
)
)
.then( updatedDocs => {
// do something with array of updated documents
})
.catch(e => console.error(e))
另一个“调整”可能是使用 的“bluebird”实现Promise.map()
,它结合了 (s) 实现的通用Array.map()
性Promise
和控制运行并行调用的“并发性”的能力:
const Promise = require("bluebird");
Model.find({},{ _id: 1, tweet: 1}).then(tweets =>
Promise.map(tweets, ({ _id, tweet }) =>
api.petition(tweet).then(result =>
TweetModel.findOneAndUpdate({ _id }, { result }, { new: true })
),
{ concurrency: 5 }
)
)
.then( updatedDocs => {
// do something with array of updated documents
})
.catch(e => console.error(e))
“并行”的替代方案将按顺序执行。如果太多的结果导致太多的 API 调用和调用写回数据库,则可以考虑这样做:
Model.find({},{ _id: 1, tweet: 1}).then(tweets => {
let updatedDocs = [];
return tweets.reduce((o,{ _id, tweet }) =>
o.then(() => api.petition(tweet))
.then(result => TweetModel.findByIdAndUpdate(_id, { result }, { new: true })
.then(updated => updatedDocs.push(updated))
,Promise.resolve()
).then(() => updatedDocs);
})
.then( updatedDocs => {
// do something with array of updated documents
})
.catch(e => console.error(e))
在那里,我们可以使用Array.reduce()
将 Promise“链接”在一起,让它们按顺序解决。请注意,结果数组保持在范围内,并用最终.then()
附加到连接链末尾的结果进行交换,因为您需要这种技术来“收集”来自 Promises 在该“链”中不同点解析的结果。
异步/等待
在从 NodeJS V8.x 开始的现代环境中,这实际上是当前的 LTS 版本并且已经有一段时间了,你实际上已经支持async/await
. 这使您可以更自然地编写流程
try {
let tweets = await Model.find({},{ _id: 1, tweet: 1});
let updatedDocs = await Promise.all(
tweets.map(({ _id, tweet }) =>
api.petition(tweet).then(result =>
TweetModel.findByIdAndUpdate(_id, { result }, { new: true })
)
)
);
// Do something with results
} catch(e) {
console.error(e);
}
如果资源是一个问题,甚至可能按顺序处理:
try {
let cursor = Model.collection.find().project({ _id: 1, tweet: 1 });
while ( await cursor.hasNext() ) {
let { _id, tweet } = await cursor.next();
let result = await api.petition(tweet);
let updated = await TweetModel.findByIdAndUpdate(_id, { result },{ new: true });
// do something with updated document
}
} catch(e) {
console.error(e)
}
还要注意,findByIdAndUpdate()
也可以用作匹配_id
已经隐含的,因此您不需要将整个查询文档作为第一个参数。
大写
最后一点,如果您实际上根本不需要更新的文档来响应,那么bulkWrite()
这是更好的选择,并且允许写入通常在单个请求中在服务器上处理:
Model.find({},{ _id: 1, tweet: 1}).then(tweets =>
Promise.all(
tweets.map(({ _id, tweet }) => api.petition(tweet).then(result => ({ _id, result }))
)
).then( results =>
Tweetmodel.bulkWrite(
results.map(({ _id, result }) =>
({ updateOne: { filter: { _id }, update: { $set: { result } } } })
)
)
)
.catch(e => console.error(e))
或通过async/await
语法:
try {
let tweets = await Model.find({},{ _id: 1, tweet: 1});
let writeResult = await Tweetmodel.bulkWrite(
(await Promise.all(
tweets.map(({ _id, tweet }) => api.petition(tweet).then(result => ({ _id, result }))
)).map(({ _id, result }) =>
({ updateOne: { filter: { _id }, update: { $set: { result } } } })
)
);
} catch(e) {
console.error(e);
}
上面显示的几乎所有组合都可以变成这样,因为该bulkWrite()
方法需要一个指令“数组”,因此您可以从上面每个方法的处理过的 API 调用中构造该数组。
推荐阅读
- django - 如何在 django 中间件中添加字段以请求正文
- node.js - 导出在异步函数中声明的函数
- c# - 从 ASP.NET Core 2 控制器返回 PDF
- javascript - 无法迭代对象数组(未定义错误)
- python - 在 Ubuntu 18.04 和 Python 3 中运行命令时出错
- firebase - firebase deploy --only 功能上出现 NPM 错误的原因是什么
- python - 获取仅声明的类/实例属性(不继承)?
- spring - 为 Rest Api 实现 Spring Security
- php - JWT 多重认证
- spring - @PostConstruct 方法在 flyway 之前运行