I have a peice of code which loops through an array (sql_RS_JS_pagelisting.data
) and then triggers a query using a value from the array(page_uuid
)
I trigger the query inside a promise which ensures the results are returned.
Once the results are returned, I then loop through an array (response.data.http_monitors.map
) in the results and insert into the array ( httpuuid: item.uuid
) along with some of the contents of the original array (eg. webuuid: web_uuid
)
This works absolutely perfectly
const arr = sql_RS_JS_pagelisting.data //your array of things to iterate over
const query = API_RS_JS_IPM // your query to trigger for each
const promises = arr.map((item) => {
const web_uuid = item.webuuid;
const web_name = item.webname;
const page_uuid = item.uuid;
const page_name = item.label;
return query.trigger({
additionalScope: {
jspage: page_uuid
}
}
)
.then(
response => {
const updatedResults =
response.data.http_monitors.map(resultItem => ({
...resultItem,
webuuid: web_uuid,
webname: web_name,
pageuuid: page_uuid,
pagename: page_name,
httpuuid: item.uuid
}));
return { results: updatedResults };
}
);
}
);
return Promise.all(promises)
.then(resultsArray => resultsArray);
However, now I want to do something similar (ie. return an array to combine the first array eg, webuuid, webname
, etc. with the results of the query) - however, this time, the results are not in an array (as it is a 1:1 relationship).
Simply the value I need is in data.ipm_monitor.uuid
So effectively all I need is to return an array in the promise with
webuuid (from the first array)
and data.ipm_monitor.uuid
However, Im really struggling to do this.
Can you please help?