Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
Reference changed before change of reference

What I am trying to do is to switch out an object's property (a string) with a matching (keyed) object from another object where the values are keyed.

So for example...

const dataRefs = {
  'idkey1': { title: "Key1", /*...etc... */ },
  'idkey2': { title: "Key2", /*...etc... */ },
  'idkey3': { title: "Key3", /*...etc... */ },
  // ...etc...
};

const pages = [
 { title: "A Page", data: 'idkey1' },
 // ...etc...
];

Using the below code I want to switch out pages[n].data with the matching property in dataRefs. So using a forEach on the pages...

pages.forEach(page => page.data = dataRefs[page.data])

Doing this results in page.data property becoming undefined, even though it should match.

If I try to debug by outputting it to console, I get some unusual effect of seeing the undefined only when the code is added after the output....

// This works and does the match exactly as I want it.
pages.forEach(page => console.log("%s: ", page.data, dataRefs[page.data]));
// Output:
//  idkey1: undefined

// This however results in bizzare behaviour and ends up with "undefined".
pages.forEach(page => {
  // using console.log to see what's going on...
  console.log("%s: ", page.data, dataRefs[page.data]);
  page.data = dataRefs[page.data];
});
// Output:
//  [Object object]: undefined

// Trying this alternative, just in case how the DOM inspector 
// might be using references, but still the same issue...
pages.forEach(page => {
  console.log(page.data + ": ", dataRefs[page.data]);
  page.data = dataRefs[page.data];
});
// Output:
//  [Object object]: undefined

Have checked spelling of variables and gone over and over the code trying so many variants but it seems that no matter what I do, calling page.data = dataRefs[page.data] does not work. Would this be some sort of complex race-condition or have I been watching too much Matrix of late?

This is being called in the Component's render() method.

Using Safari 14.1.2, if that helps.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The issue was related with Next.JS. Best guess is that Next.JS was pre-rendering the data, storing it in some JSON cache file and passing that to the component render function....or something like that.

Using the browser's inspector, a breakpoint at the problematic line page.data = dataRefs[page.data] was only ever triggered once, and showed the data had already been manipulated by the function, before it had been called. Which is simply odd. Removing the line, the breakpoint would trigger and the data not converted.

This leads me to believe it to be some sort of NextJS pre-lifecycle thing, possibly as part of the SSG process.

To resolve the issue and move on, I used a check if (page.data instanceof Object) return; to stop it from running twice, which seemed to do the trick. Not ideal, but without a better reason, this will have to suffice. So the code ultimately went like....

pages.forEach(page => {
   // skip the loop if the data has already been converted
   //   could have also used a string check, but both should work for now.
   if (page.data instanceof Object) return;

   // now change the data if it's still a string referrence
   page.data = dataRefs[page.data]));
});

Again, appologies that I don't have the best answer, but this was the only way to resolve it, and since Javascript does not do this normally (have done this sort of thing so many times without issue), it will have to be chalked up to a NextJS/SSG (or some other processor) issue.

Would love to get any NextJS expert's knowledge on how this could happen.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda