I am working with an array of products in typescript (in angular) and my goal is to update their prices in the database using http calls.
The way I currently insert them is as follows:
array.foreach(element=>{
this.product.price=element;
this.myService.func(this.product).subscribe(a=>{console.log(a);});
})
However, the issue with this code is that the final data is incorrect due to server response times. This means that for the last two objects in the array, the same price as the last member of the array will be sent to the server.
To provide a clearer example:
Let's say the array has initial prices like this:
[0]=10
[1]=20
[2]=30
In addition to this array, I have a product instance:
Product p = new Product('product',50);
Now, my objective is to add 3 more products to the database with the name 'product' and the corresponding prices from the array, using a function from the server.
The desired outcome is all the products inserted into the database:
product,10
product,20
product,30
Unfortunately, I am getting a different result than expected:
product,10
product,30
product,30
Is there a way to fix this issue?
Thank you for your assistance.