gpt4 book ai didi

javascript - 将数据添加到 firebase 并同时重定向到其他页面

转载 作者:行者123 更新时间:2023-12-03 01:28:20 25 4
gpt4 key购买 nike

我正在使用 Firebase 创建一个具有登录/注册功能的网站,问题是当用户创建新帐户时,我想将他重定向到主页并将他的名字放入 firebase数据库同步。用户被重定向到主页,我无法将数据添加到 firebase 数据库

请检查我的代码

signUp.addEventListener('click',() => {

const emailValue = emailField.value;
const passValue = passField.value;

if(emailValue === "" && passValue === "" || emailValue !== "" && passValue == "" || emailValue == "" && passValue !== "")
{
alert('Please fill all fields')
}
else {
const auth = firebase.auth();
const promise = auth.createUserWithEmailAndPassword(emailValue,passValue);
promise
.then(() => {
auth.signInWithEmailAndPassword(emailValue,passValue);
let userId = auth.currentUser.uid;
let dataRef = firebase.database().ref('users/' + userId);
dataRef.set({
name: nameField.value
});
console.log('User Signed In');
return './homepage.html';
})
.then((address) => {
window.location.href = address;
})
.catch(e => {
console.log(e.message);
})

}

});

提前致谢:)

最佳答案

createUserWithEmailAndPassword 返回一个用户对象,因此无需重新验证凭据。请尝试这个:

signUp.addEventListener('click', () => {
const emailValue = emailField.value;
const passValue = passField.value;
if (emailValue === "" && passValue === "" || emailValue !== "" && passValue == "" || emailValue == "" && passValue !== "") {
alert('Please fill all fields')
} else {
firebase.auth().createUserWithEmailAndPassword(emailValue, passValue).then(function (user) {
const user_data = {
wo: 0
};
const promise = firebase.app().database().ref(`/users/${user.uid}`).set(user_data);
promise.then(() => {
setTimeout(1000);
window.location.href = './homepage.html';
});
}, function (error) {
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorMessage);
});
}
});

关于javascript - 将数据添加到 firebase 并同时重定向到其他页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51407798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com