0
I have an array with the Events to be searched, to check if there is another one on the same day and time.
The code below returns me the existing events without problem, but only on the console.log. I must return to the Front and for now I’m not succeeding.
In short, my loop does not return the json I need to the front.
const searchEvent = async event => {
const response = await Event.findOne({
where: {
[Op.or]: [
{
start: { [Op.between]: [event.start, event.end] },
},
{
end: { [Op.between]: [event.start, event.end] },
},
],
room_id: event.room_id,
},
});
return response;
};
const loadEvents = arrayOfEventsWithDateIniAndDateEnd.map(
async event =>
await searchEvent(event).then(result => console.log(result))
);
return res.json(loadEvents);
Opa @Luiz Felipe. I made the change but it didn’t work. In India you have the error: 500 Internal Server Error [Object Promise]. And on the console displays the error: "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client..."
– Thiago Augusto Monteiro Pontes
You need to handle any rejections. Try to use
try/catch
, for example. Do not forget that, if one of the promises reject, the one returned byPromise.all
will also be rejected.– Luiz Felipe
I used Try/catch, me returns the : Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client. And the function is working because I can see the result on the console when I use the console.log.
– Thiago Augusto Monteiro Pontes
You must be setting some header in another part of the application... It has nothing to do with the
Promise.all
(or anything from this question or answer). Learn more about this error here.– Luiz Felipe
This controller is new, has little code, I treated all with "Return". But for testing, I removed all the res.json from it, leaving only the import of the Model and the sequelize with code that I posted. Still presents the error.
– Thiago Augusto Monteiro Pontes
you are right. The problem is in the conditional routes I am using. I tested commenting and it worked. Now the problem is with the route. kkkkk
– Thiago Augusto Monteiro Pontes