【问题标题】:Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'emailAddress')Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'emailAddress')
【发布时间】:2022-07-22 04:28:32
【问题描述】:

我正在以下方法中解析来自 CSV 的几封电子邮件,并且我收到无法读取未定义的属性(正在读取“电子邮件地址”)。我什至尝试过滤掉未定义的结果,但没有运气。如何过滤未定义的。

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

【问题讨论】:

  • 当你做.filter(x =&gt; isValidEmail(x.emailAddress))x可能是undefined)?

标签: javascript reactjs


【解决方案1】:

在您的代码中:

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

您在这些行上隐式返回什么/未定义:

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

相当于返回undefined。然后在随后的 filter 中,您假设 x.emailAddress 存在,但它可能不存在,就像上面您在某些极端情况下返回 undefined 一样。

为了补救您:

  1. 必须更改您的 filter 函数(可能是最好的解决方案)
  2. 使isValidEmail 函数期望整个电子邮件对象而不是期望的字符串(可能不太理想)

如果您选择了第一种方法,它应该类似于:

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

应该可以。

【讨论】:

  • “这是没用的(上面已经介绍过)”。不完全是,因为 !emailAddress 使用 &amp;&amp; 运算符和另一个变量进行检查。因此,如果您有一个 displayName 具有一些真正的价值并且没有 emailAddress 存在,代码将继续到最后(没有第二个 if 语句)。也许在这种情况下使用|| 会更好,但事实上,第二个 if 也不是没用的。
  • 很公平的@Doc,我也认为email/displayName 的检查与|| 相比更有意义(如果其中一个错误,我们必须停止),所以更改了代码反映这一点。
猜你喜欢
  • 2022-01-11
  • 1970-01-01
  • 2022-10-09
  • 2022-07-21
  • 2022-10-13
  • 2022-01-07
  • 2022-10-18
  • 2021-11-10
  • 2022-06-13
相关资源
最近更新 更多