I am trying to replace an instance of hardcoding in a web application with 2 very similar API calls, as I don't have time to rewrite a whole new procedure and endpoint to handle the same functionality right now. I am able to get the data from the 2 individual calls fine with Promise.all
. But the template for this page (it is using handlebars) is expecting a single object to loop through and display the options in a drop down.
I have been trying to use spread operators to bring the contents of the results together with no duplicates, and it does work as expected if I mock it up synchronously in the console, but in practice the results get nested into another object as if it were an array.
This is the code actually running:
Promise.all([
this.isUsers = GetUsersByRole("Inside Sales", app.user.id),
this.asmUsers = GetUsersByRole("Area Sales Managers", app.user.id)
]).then((is, asm) => {
// is and asm contain the correct data here, I just want to merge it
this.users = {...is, ...asm};
var dummy = {...is, ...asm};
console.log("dummy", dummy);
});
In the console, manually copying the data from each successful data grab into its own object and then combining with the spread operator as above gives the expected result. But this code returns some sort of nesting of it instead:
If the spread operator (or using Object.attach, which I have also tried) worked as expected, I believe I would have the result I'm looking for. I don't know how many little hacks I've tried to merge the objects properly, but none seem to have the right behavior in this context.
I may just need some more reading on Promises and async operations, so feel free to link me
Via Active questions tagged javascript - Stack Overflow https://ift.tt/w71lyAH
Comments
Post a Comment