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

115
Vistas
I can't access a property of an object

I'm accessing an object from the database. Here is the object I get:

{
 data: {
  settings: {
   language: 'en-US'
  }
 }
}

I'm trying to access the language part, but somehow it's not working, this is my code:

translate(key, args, base) {
  const language = base.data.settings.language
  const result = this.translation.get(base ? language : 'en-GB')
  ...
  return result;
}

My problem is that the object isn't defined in that result (this part: this.translation.get(base ? language : 'en-GB'))

The data is defined, I've tried console.log(language) and it's returning en-US :

const language = base.data.settings.language
console.log(language) // en-US
const result = this.translation.get(base ? language : 'en-GB') // cannot read property 'settings' of undefined

But when I change the language variable to a string like const language = "en-US" it's working for both of them. Am I doing this wrong?

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

0

I am guessing that base is an optional parameter. That explains the cannot read property 'settings' of undefined error which should actually appear on the first line of your code. You need to change your code as follows:

const language = base ? base.data.settings.language : "en-GB";

If there is a possibility that the path does not exist then the safer solution is this:

const language = base?.data?.settings?.language ?? "en-GB";
about 4 years ago · Juan Pablo Isaza Denunciar

0

const result = this.translation.get(base ? language : 'en-GB')

In the ^above line, you pass the parameter after checking if the base is nullish or not.

But At the same time in previous line, const language = base.data.settings.language you already access the base object regardless of it being nullish. That doesn't make sense.

Also,

but if I change the language variable to a string like const language = "en-US" it's working for both of them, am I doing this wrong?

^This means the issue is with that line

I suggest you to add the nullish check there, like below:

const language = base ? base.data.settings.language : 'en-GB';
const result = this.translation.get(language)
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