Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 类型错误:Can';t读取属性';电子邮件';未定义的_Javascript_Reactjs - Fatal编程技术网

Javascript 类型错误:Can';t读取属性';电子邮件';未定义的

Javascript 类型错误:Can';t读取属性';电子邮件';未定义的,javascript,reactjs,Javascript,Reactjs,当我使用正确的电子邮件和密码时,它工作正常 但当电子邮件和密码不正确时,我得到:无法读取未定义的属性“email”错误。我想在if/else部分处理它 请帮忙。谢谢 const{customers}=useContext(CustomerContext) 常量[customerEmail,setCustomerEmail]=useState(“”) const[password,setPassword]=useState(“”) const history=useHistory(); const

当我使用正确的电子邮件和密码时,它工作正常

但当电子邮件和密码不正确时,我得到:
无法读取未定义的属性“email”
错误。我想在
if/else
部分处理它

请帮忙。谢谢

const{customers}=useContext(CustomerContext)
常量[customerEmail,setCustomerEmail]=useState(“”)
const[password,setPassword]=useState(“”)
const history=useHistory();
const currentUser=customers.find((s,index)=>s.email==customerEmail)
常量handleLogin=(e)=>{
e、 预防默认值()
如果(currentUser.email==customerEmail&¤tUser.password==password)
{
localStorage.setItem(“客户”,currentUser.id)
历史记录。推送(“/主页”)
道具
}
else if(typeof currentUser.email==未定义)
{
window.alert(“电子邮件不匹配”)
}
否则{
window.alert(“未找到匹配项”)
}
}
返回(
客户邮件
setCustomerEmail(e.target.value)}/>
暗语
setPassword(e.target.value)}/>
提交
)

通常,
handleLogin
方法的逻辑条件可能是这样的:

if(currentUser==未定义){
window.alert(“未找到匹配项”)
//您不应该告诉用户登录/密码组合中的哪一部分不匹配,因为这将使强制尝试变得更容易
}else if(currentUser.email!==customerEmail | | currentUser.password!==password){
window.alert(“电子邮件或密码不匹配”)
}否则{
localStorage.setItem(“客户”,currentUser.id)
历史记录。推送(“/主页”)
道具
}
我希望您不要在前端依赖此代码。因为验证登录和
这样的密码=)

如果
customers.find(…)
没有返回任何东西(给它一个错误的电子邮件/密码),那么你就不能使用
currentUser.email
,因为
currentUser
null
。一个简单的
if(!currentUser){…}
应该在这里有所帮助:)谢谢你的重播。我使用了if(!currentUser){window.alert(“找不到匹配”)},但仍然是相同的错误消息,您必须阻止进一步执行;警报不能做到这一点。在
handleLogin()
函数的某个地方,您必须执行
if(!currentUser){…}else{…}