Hi folks,
I have a flow where I query the chripstack rest api for all devices through a http in node. To do this I have to query /applications to get the total applications, then query /applications?limit= then for each application I send a new http request asking for its number of applications and then list all of them.
My problem begins when I loop through all applications to send a new http request for each. I can not guarantee that they are in order or that all requests have succeeded. Thus, if they all succeed everything is fine, but if some fail I can't join them with my join node relying on the parts property.
I want to respond to the original http request that something went wrong, but I need to make sure I do not let the other requests progress through the flow if one of the parts fail. One option is to do all the fetchin inside the function node and use async/await, but I do not really want that.
Below is a picture of the flow and the code for the requestPerApplication function node that creates the requests that causes my headache.
// Generate UUID
const randId = crypto.randomUUID();
// The number of applications
const count = parseInt(msg.numberOfApplications);
// Storing the result
const result = msg.payload.result;
// Request all the devices for each application
result.forEach((item, index) => {
const newMsg = {
"payload":{},
"url": `${baseUrl}&applicationID=${item.id}`,
"method": "GET",
"req": msg.req,
"res": msg.res,
"_msgid": msg._msgid,
"parts": { "id": randId, "index": index + 1, "count": count, "type": "array", "key": "msg.payload.result"}
};
// Send the request
node.send(newMsg);
});
// All requests are sent
node.done();