我正在尝试在find方法返回结果之前从远程源更新数据库。我试图在“查找”操作上使用"beforeRemote“方法。它正在更新数据库,但它不会等到数据库更新完成后才返回数据。我可以判断,因为当我第一次对一个空数据库调用"find“端点时,结果是空的,但是在检查调用之后的数据库时,它中有数据。
下面是我的模型类(删除敏感内容)。
'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);
});
};我是新来的,所以我肯定这是个简单的错误。我做错了什么?
发布于 2018-10-21 17:37:59
使用Promise.all Promise.all
您不应该在异步操作中使用forEach --在您的情况下是更新--因为forEach不会等待更新完成,然后调用next。相反,可以使用map来循环事件并返回更新操作的承诺,这样您就可以创建承诺数组并使用Promise.all。在中,然后调用Promise.all的调用下一个方法。
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);
});
};或者您可以使用异步/等待异步/等待,这是更容易读的,而且您不需要调用next回环,就会为您处理这个问题。
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);
});
};https://stackoverflow.com/questions/52917928
复制相似问题