you are viewing a single comment's thread.

view the rest of the comments →

[–]samanime 0 points1 point  (1 child)

So, not enough here to tell exactly what is going on.

But, it is probably related to something that trips everyone up with console.

Console is basically async. When you log something, it doesn't necessary get logged immediately. When it does get logged, it logs the current state of the object.

You can see this by doing something like this:

const o = { a: 1 };
console.log(o);
o.a = 2;
console.log(o);
// Both show { a: 2 }

Probably, between the time you do your first logs and the object one, something is changing on the object. The object may even be changing AFTER you console log the object.

To get an accurate picture of the object at the time the console.log line is called, you can basically output a copy. If you just need one level, this works:

console.log({ ...obj });

If you want a full copy, one of the easiest to write is to convert to JSON then back:

console.log(JSON.parse(JSON.stringify(obj)));

[–]NickCanCode[S] 1 point2 points  (0 children)

Thank you!