我试图在 find 方法返回结果之前从远程源更新数据库。我试图在“查找”操作上使用“beforeRemote”方法。它正在更新数据库,但它不会等到数据库更新完成才返回数据。我可以这么说,因为我第一次针对空数据库调用“查找”端点时,结果为空,但在调用后检查数据库时,其中包含数据。
这是我的模型类(删除了敏感内容)。
'use strict';
var Cronofy = require('cronofy');
var _ = require('lodash');
module.exports = function (Event) {
// remote method before hook
Event.beforeRemote('find', function (ctx, unused, next) {
var client = new Cronofy({
access_token: 'secret-token',
});
var options = {
from: "2018-10-15",
to: "2018-11-15",
tzid: 'Etc/UTC'
};
client.readEvents(options)
.then(function (response) {
var returnedEvents = response.events;
var events = _.filter(returnedEvents, function(o){
return !_.isEmpty(o.summary) && !_.isEmpty(o.event_uid) && !_.isEmpty(o.start) && !_.isEmpty(o.end);
});
events.forEach(element => {
Event.upsertWithWhere({
sourceType: "external-source-a",
sourceID: element.event_uid
}, {
sourceType: "external-source-a",
sourceID: element.event_uid,
summary: element.summary,
description: element.description,
start: element.start,
end: element.end,
recurring: element.recurring
},
function (err, model) {
if (err) {
console.log(err);
}
//console.log(model);
}
);
});
next();
}).catch(console.log);
});
};
我是环回新手,所以我确信这是一个简单的错误。我做错了什么?
最佳答案
使用 Promise.all Promise.all
在更新的情况下,您不应该将 forEach 与异步操作一起使用,因为 forEach 不会等待更新完成,然后调用 next。 相反,使用 map 来循环事件并返回更新操作的 Promise,以便您可以创建 Promise 数组并使用 Promise.all。 在 Promise.all 的 then 中调用 next 方法。
module.exports = function (Event) {
// remote method before hook
Event.beforeRemote('find', function (ctx, unused, next) {
var client = new Cronofy({
access_token: 'secret-token',
});
var options = {
from: "2018-10-15",
to: "2018-11-15",
tzid: 'Etc/UTC'
};
client.readEvents(options)
.then(function (response) {
var returnedEvents = response.events;
var events = _.filter(returnedEvents, function(o){
return !_.isEmpty(o.summary) && !_.isEmpty(o.event_uid) && !_.isEmpty(o.start) && !_.isEmpty(o.end);
});
const updatepromises = events.map((element) => {
return Event.upsertWithWhere({
sourceType: "external-source-a",
sourceID: element.event_uid
}, {
sourceType: "external-source-a",
sourceID: element.event_uid,
summary: element.summary,
description: element.description,
start: element.start,
end: element.end,
recurring: element.recurring
});
});
return Promise.all(updatepromises);
})
.then((result) => next())
.catch(console.log);
});
};
或者你可以使用 async/await async/await它更具可读性,您不必调用下一个环回,它会为您处理好。
module.exports = function (Event) {
// remote method before hook
Event.beforeRemote('find', async function (ctx, unused) {
var client = new Cronofy({
access_token: 'secret-token',
});
var options = {
from: "2018-10-15",
to: "2018-11-15",
tzid: 'Etc/UTC'
};
var response = await client.readEvents(options);
var returnedEvents = response.events;
var events = _.filter(returnedEvents, function (o) {
return !_.isEmpty(o.summary) && !_.isEmpty(o.event_uid) && !_.isEmpty(o.start) && !_.isEmpty(o.end);
});
const updatepromises = events.map((element) => {
return Event.upsertWithWhere({
sourceType: "external-source-a",
sourceID: element.event_uid
}, {
sourceType: "external-source-a",
sourceID: element.event_uid,
summary: element.summary,
description: element.description,
start: element.start,
end: element.end,
recurring: element.recurring
});
});
await Promise.all(updatepromises);
});
};
关于node.js - 环回 - "next()"未在 "beforeRemote"中的正确时间触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52917928/