Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!