undefined 不是对象(正在计算 'userCredentails.user')
undefined is not an object (evaluating 'userCredentails.user')
我正在为我的 React 本机应用构建用户身份验证页面。注册我的用户时无法更新显示名称的问题。之后,我需要将姓名、电子邮件和密码存储在我的 Firestore 数据库中。
我正在使用 expo 和 firebase。我不明白为什么它不起作用。
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()
函数 returns 一个承诺。尝试用 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)
}
}
我正在为我的 React 本机应用构建用户身份验证页面。注册我的用户时无法更新显示名称的问题。之后,我需要将姓名、电子邮件和密码存储在我的 Firestore 数据库中。
我正在使用 expo 和 firebase。我不明白为什么它不起作用。
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()
函数 returns 一个承诺。尝试用 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)
}
}