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

548
Views
TypeError: Cannot read properties of undefined (reading 'id')

I have this error in my terminal:

TypeError: Cannot read properties of undefined (reading 'id')

I'm trying to test the call to an API, but the error appears.

My function:

itemToForm = () => {
    this.api.send(this.component, 'get',
        { lang: 'ES', filter: { id: this.item['id'] } }
    ).then(resEsp => {
        this.item = resEsp['data'][0];
        this.api.send(this.component, 'get',
            { lang: 'EN', filter: { id: this.item['id'] } }
        ).then(res => {
            let itemEng = res['data'][0];
            let fields = this.formDef.map(register => register.filter(
                field => field['register_table'].indexOf('traduction') !== -1
            ).map(
                field => field['field_name'])
            ).filter(register => register.length);

            fields = fields.length ? fields[0] : [];

            if (itemEng) {
                this.item = Object.keys(itemEng).reduce((obj, key) => {
                    obj[key] = this.item[key];
                    if (fields.indexOf(key) !== -1) {
                        obj[key + '_eng'] = itemEng[key];
                    }
                    return obj;
                }, {});
            }

            if (this.item) {
                this.setForm();
            }
        })
    })
}

My specification file:

it('should call api.send', () => {
    let spy1 = spyOn(api, 'send');
    let item = {
        id: 1,
        name: 'test',
    }

    component.addItem(item);
    component.itemToForm();

    expect(spy1).toHaveBeenCalled();
});
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I bumped to this question but my issue was actually something completely different.

In my code for some reasons I had

import { SOME_OBJECT } from '.';

which should instead be like this:

import { SOME_OBJECT } from './proper-file';
over 4 years ago · Santiago Trujillo Report

0

What is happening:

The function itemToForm() is being called before the this.item is ready.

There are many strategies to avoid this error. A very simple one is to add a catcher at the beginning of the function, like this:

itemToForm = () => {
  if(this.item === undefined) {return}
         
  // The rest of the code
}

This stops the function, if the data does not exist yet.

A more elegant solution may be to go further up in your order of operations, and find who is calling itemToForm() and ensure the data exists prior to calling.

over 4 years ago · Santiago Trujillo Report

0

In case it's useful for anyone to know, I got the error Cannot read properties of undefined (reading 'spec') when attempting to install a local npm module with npm install [path to module here]. The source of the problem: I hadn't given the local module a name in its package.json.

over 4 years ago · Santiago Trujillo 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!