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

440
Views
TypeError: no se pueden leer las propiedades de undefined (leyendo 'id')

Tengo este error en mi terminal:

TypeError: no se pueden leer las propiedades de undefined (leyendo 'id')

Estoy intentando probar la llamada a una API, pero aparece el error.

Mi función:

 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(); } }) }) }

Mi archivo de especificaciones:

 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
2 answers
Answer question

0

Lo que está sucediendo:

Se llama a la función itemToForm() antes de que this.item esté listo.

Hay muchas estrategias para evitar este error. Una muy simple es agregar un receptor al comienzo de la función, así:

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

Esto detiene la función, si los datos aún no existen.

Una solución más elegante puede ser ir más arriba en su orden de operaciones y encontrar quién está llamando a itemToForm() y asegurarse de que los datos existan antes de llamar.

over 4 years ago · Santiago Trujillo Report

0

Me encontré con esta pregunta, pero mi problema era en realidad algo completamente diferente.

En mi código por algunas razones tuve

 import { SOME_OBJECT } from '.';

que en su lugar debería ser así:

 import { SOME_OBJECT } from './proper-file';
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!