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

567
Views
Angular 7: TypeError: no se pueden leer las propiedades de undefined (leyendo 'substr')

Tengo una aplicación angular 7.1.4 que se compila correctamente, pero cuando abro la consola en mi navegador, veo este error:

 ERROR TypeError: Cannot read properties of undefined (reading 'substr') core.js:14597 at TruncatePipe.push../src/app/_services/truncate.ts.TruncatePipe.transform (truncate.ts:11:21)

Si entiendo esto correctamente, hay algo mal con el siguiente código en mi archivo truncate.ts. Aquí está mi código:

 import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'truncate' }) export class TruncatePipe implements PipeTransform { transform(value: string, limit = 25, completeWords = false, ellipsis = '...') { if (completeWords) { limit = value.substr(0, limit).lastIndexOf(' '); } return `${value.substr(0, limit)}${ellipsis}`; } }

No estoy seguro de lo que está mal aquí? ¿Alguna idea de lo que podría estar mal?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

sobre todo añade una condición

 if(value){ ....your code }
about 4 years ago · Juan Pablo Isaza Report

0

Indraraj26 señala que debe envolver su código en un valor de verificación antes de ejecutarlo.

Si el valor alguna vez es nulo, no puede tomar una subcadena del mismo, por lo que para evitar este escenario, debe verificar si el valor es != nulo o "". Puede hacerlo simplemente en Angular usando

 import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'truncate' }) export class TruncatePipe implements PipeTransform { transform(value: string, limit = 25, completeWords = false, ellipsis = '...') { if(value){ if (completeWords) { limit = value.substr(0, limit).lastIndexOf(' '); } return `${value.substr(0, limit)}${ellipsis}`; } } }

La declaración if será falsa para nulo y evitará que su código se ejecute y, por lo tanto, falle.

about 4 years ago · Juan Pablo Isaza Report

0

Antes de usar el value , debe verificar si no está undefined . Mire el siguiente ejemplo:

función:

 public validateText(text: string) { return text; }

Llamar a esta función ahora causará un error:

 const text = 'just an example' as string | undefined; validateText(text);

error:

 TS2322: Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.

En su caso, debe verificar el valor antes de llamar a la función o dentro de ella antes de substr o ser más específico de la siguiente manera:

 if (typeof value === 'string') { //Your code } // OR in short if (value) { //Your code }
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!