reactjs - undefined 不是对象(评估 'userCredentails.user')
问题描述
我正在为我的 react 本机应用程序构建用户身份验证页面。问题我在注册用户时无法更新 displayName。之后,我需要将姓名、电子邮件、密码存储在我的 Firestore 数据库中。
我正在使用世博会和火力基地。我不明白为什么它不起作用。
import firebase from "firebase";
import { auth } from "../../firebase";
const SignUpScreen = () => {
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
// const user = firebase.auth().currentUser;
// Sign Up ----------
const handleSignUp = () => {
auth
.createUserWithEmailAndPassword(email, password)
.then((userCredentials) => {
if (userCredentials) {
const user = userCredentials.user;
user.updateProfile({
displayName: name,
});
}
})
.then((userCredentials) => {
const user = userCredentials.user;
// console.log("Registered with:", user.email);
})
.catch((error) => alert(error.message));
};
解决方案
第一个then()
块似乎没有返回任何东西。函数也updateProfile()
返回一个承诺。尝试用 async-await 语法重构代码,如下所示:
const handleSignUp = async () => {
try {
const userCredential = await auth.createUserWithEmailAndPassword(email, password)
const user = userCredential.user
await user.updateProfile({ displayName: name })
console.log("Registered with:", user.email);
} catch (e) {
console.log(e)
}
}
推荐阅读
- reporting-services - SSIS/SSRS - 当我在 VS 2017 中为 SSIS 创建一个新项目时出现此错误
- r - Can't connect to CRAN mirror from RStudio
- sql - achieve "friends data model" with postgres
- flutter - Flutter:使用 Radio 选择单个项目
- java - How to darken CardView using onClick?
- express - express-generator 是否在脚手架完成后添加视图?
- c - Relocation of data from flash to RAM during boot phase
- c# - Get the realtime CPU speed in C#?
- python - Check if the sum of 2 numbers in a list is present in another list in python
- mysql - 如何为 ExcelforMySQL 添加到 Ionos 数据库的远程连接?