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

239
Views
No capturado (en promesa) TypeError: no se pueden leer las propiedades de undefined (leyendo 'emailAddress')

Estoy analizando algunos correos electrónicos de un CSV en el método a continuación y obtengo No se pueden leer las propiedades de undefined (leyendo 'emailAddress'). Incluso intenté filtrar los resultados indefinidos, pero no tuve suerte. ¿Cómo puedo filtrar los que no están definidos?

 const getContactsFromText = (text) => { if(text == "" || text.trim() == '' || text === undefined){ settingTheAlert(); return; } const contacts = text.split(/[,;\n]/) .filter(x => x.length > 0) .map(x => x.trim()) .map(x => { const [, , displayName = '', emailAddress = ''] = x.match(/"?((.*?)"?\s*<)?([^">]*)/); if (!emailAddress && !displayName) return; if(emailAddress === undefined) return; return { id: emailAddress, emailAddress, displayName, isChecked: true }; }) .filter(x => isValidEmail(x.emailAddress)) .sort(sortByEmail); if(contacts.length < 1){ settingTheAlert(); return; } onImport(contacts); } const isValidEmail = (email) => { const EMAIL_RE = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; const isValid = EMAIL_RE.test(String(email).toLowerCase()); if (!isValid) console.log('invalidEmail', { email }) return isValid; }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En tu código aquí:

 .map(x => { const [, , displayName = '', emailAddress = ''] = x.match(/"?((.*?)"?\s*<)?([^">]*)/); if (!emailAddress && !displayName) return; if (emailAddress === undefined) return; // this is useless (it's covered above) return { id: emailAddress, emailAddress, displayName, isChecked: true }; }; })

implícitamente no devuelves nada/indefinido en estas líneas:

 if (!emailAddress && !displayName) return; if (emailAddress === undefined) return; // again, this line is not needed

que es equivalente a devolver undefined . Luego, en el filter subsiguiente, asume que x.emailAddress existe, pero es posible que no, ya que justo arriba estaba devolviendo indefinido en algunos casos de esquina.

Para remediarlo usted:

  1. Tengo que cambiar la función de filter (probablemente la mejor solución)
  2. Haga que la función isValidEmail espere todo el objeto de correo electrónico en lugar de la cadena esperada (probablemente un poco menos deseable)

Si obtuviste el primer enfoque, debería verse algo como:

 // ... other code .map(x => { const [, , displayName = '', emailAddress = ''] = x.match(/"?((.*?)"?\s*<)?([^">]*)/) if (!emailAddress && !displayName) { return null } return { id: emailAddress, emailAddress, displayName, isChecked: true } }) .filter(emailObj => emailObj && isValidEmail(emailObj.emailAddress)) // checking first that the emailObj is not undefined and then checking if the emailObj.emailAddress is valid.

Deberia trabajar.

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!