reactjs - 无法读取未定义的属性(读取“updateProfile”)
问题描述
我目前正在使用 react-redux 框架开发一个项目,我必须创建一个登录页面,并且我必须保存这些登录信息......为此我必须抓住价值......但我被困在这里尽管。
这是代码,
功能登录(){
const [email, setEmail]=useState("");
const [password, setPassword]=useState("");
const [name, setName]=useState("");
const [profilePic, setProfilePic]=useState("");
const dispatch= useDispatch();
const logintoApp =(e) =>{
e.preventDefault();
auth.signInWithEmailAndPassword(email,password)
.then((userAuth)=>{
dispatch(login({
email:userAuth.user.email,
uid:userAuth.user.uid,
displayName:userAuth.user.displayName,
profileUrl:userAuth.user.photoURL,
}));
}).catch((error)=>alert(error));
};
const register =() =>{
if(!name){
return alert("Please enter a full name!")
}
auth.createUserWithEmailAndPassword(email,password)
.then((userAuth)=>{
userAuth.user.updateProfile({ <<====here is the problem=====>>
displayName:name,
photoURL:profilePic,
})
.then(()=>{
dispatch(
login({
email:userAuth.user.email,
uid:userAuth.user.uid,
displayName:name,
photoUrl:profilePic,
})
);
});
})
.catch((error)=>alert(error.message));
};
return (
<div className='login'>
<img
src="https://www.nicepng.com/png/detail/381-3813396_linkedin-ads-are-great-for-b2b-marketing-linkedin.png"
alt=""
/>
<form>
<input value={name} onChange={(e)=>setName(e.target.value)} placeholder="Full name (required if registering)" type="text"/>
<input value={profilePic} onChange={(e)=>setProfilePic(e.target.value)} placeholder="Profile pic URL (optional)" type="text"/>
<input value={email} onChange={(e)=>setEmail(e.target.value)} placeholder="Email" type="email"/>
<input value={password} onChange={(e)=>setPassword(e.target.value)} placeholder="Password" type="password"/>
<button type='submit' onClick={logintoApp}>Sign In</button>
</form>
<p>Not a member? {' '}
<span className="login_register" onClick={register}>Register now</span>
</p>
</div>
)
}
导出默认登录
有人知道如何解决这个问题吗?
解决方案
您可以尝试从按钮中删除表单和 type=submit 吗?这个 type=submit 所做的是提交表单并刷新它,因此您要传递给 API 的数据变得未定义
<input value={name} onChange={(e)=>setName(e.target.value)} placeholder="Full name (required if registering)" type="text"/>
<input value={profilePic} onChange={(e)=>setProfilePic(e.target.value)} placeholder="Profile pic URL (optional)" type="text"/>
<input value={email} onChange={(e)=>setEmail(e.target.value)} placeholder="Email" type="email"/>
<input value={password} onChange={(e)=>setPassword(e.target.value)} placeholder="Password" type="password"/>
<button type='button' onClick={logintoApp}>Sign In</button>
推荐阅读
- javascript - 如何打印在特定页面上花费的时间?
- java - 无法在约束布局周围移动简单按钮
- python-3.x - 如何使用键从字典中打印特定值?
- swift - 如何创建像 viewDidLoad 这样的覆盖函数?
- json - 对于 web 服务 API 的数据大小> 15 MB,spring MVC 没有响应并且没有返回错误
- powershell - Copy-Item 在 Powershell 中为 NFS 挂载的磁盘引发 UnauthorizedAccessException
- regex - 做这两个正则表达式
- python - 将 LaTeX2WP 与 Python 一起使用时出现问题 - TypeError: 'float' 对象不能被解释为整数
- oracle10g - 如何解决 Oracle 10g 中的 TNS 协议适配器错误?
- javascript - 未解决的依赖关系汇总