Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda