Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

250
Visualizações
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'emailAddress')

I am parsing a few emails from a CSV in the method below and i am getting Cannot read properties of undefined (reading 'emailAddress'). I even tried filtering the undefined results out but no luck . How can I filter the ones that are undefined.

 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 Respostas
Responde à pergunta

0

In your code here:

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

you implicitly return nothing/undefined on these lines:

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

which is equivalent to returning undefined. Then in the subsequent filter you assume the x.emailAddress exists, but it might not, as just above you were returning undefined under some corner cases.

To remedy that you either:

  1. Gotta change your filter function (probably the best solution)
  2. Make the isValidEmail function expect the whole email object rather than the expected string (probably a bit less desirable)

If you got for the first approach, it should look something like:

// ... 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.

Should work.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda