0

So I'm trying to modify an object I receive via api with more info, like so:

useEffect(async () => {
setPedidos(await getPedidos()); //Get initial data from api
 }, []);
useEffect(async () => { //gets triggered when 'pedidos' is modified
if (pedidos.length > 0) {
  console.log('example from original obj:', pedidos[0])
  let obj = [...pedidos] //copies contents of 'pedidos' to a new list
  pedidos.forEach(async (item, index) => {
    obj[index].cliente = await getCliente(obj[index].cliente) //updates 'cliente' 
  })
  console.log('log 1', obj) //logs wrole obj. Shows updated info as expected
  console.log('log 2', obj[0]) //Logs first in set. Shows original info
}
}, [pedidos]);

Logs When I later try to access new information, I get the message 'cannot read properties of undefined'.

I haven't really tried much since I have no idea what to do. There is no reason for the new object to change at all since 'let obj = [...pedidos]' makes a copy of the original object, as far as I understand.

0 Answers0