gpt4 book ai didi

javascript - 创建用户,保存数据,然后更改页面

转载 作者:行者123 更新时间:2023-11-29 21:02:56 25 4
gpt4 key购买 nike

我正在尝试在我的网页上创建一个“创建用户”功能。创建用户时,它同时存储用户写入的数据,例如姓名等。这样做之后,用户应该只为用户呈现一个页面,问题在于我的代码在用户数据(姓名等)之前。 ) 被存储后,用户将看到一个新页面。我怎样才能防止这种情况发生?

我的创建用户函数:

firebase.auth().createUserWithEmailAndPassword(email, pass).then(function() {
// User is created
const imageUrl = "images/genericProfilePicture.png";
const admin = false;
const currentTime = timeStamp();

var user = firebase.auth().currentUser;
const userId = user.uid;


firebase.database().ref('users/' + userId).set({
name: name,
email: email,
address: address,
zip: zip,
city: city,
department: department,
membership: membership,
admin: admin,
added: currentTime,
profile_picture : imageUrl
});

window.open('frontpage.php', '_top');


}).catch(function(error) {
// Error has happened
var errorCode = error.code;
var errorMessage = error.message;

alert(errorMessage);
});

试过这段代码没有任何运气,没有存储数据,也没有新页面显示:

firebase.database().ref('users/' + userId).set({
name: name,
email: email,
address: address,
zip: zip,
city: city,
department: department,
membership: membership,
admin: admin,
added: currentTime,
profile_picture : imageUrl
}).then(() => {
window.open('frontpage.php', '_top');
});

最佳答案

选项 1:

只需将 set()then() block 链接起来:

firebase.database().ref('users/' + userId).set({
name: name,
email: email,
address: address,
zip: zip,
city: city,
department: department,
membership: membership,
admin: admin,
added: currentTime,
profile_picture : imageUrl
}).then(function(){
window.open('frontpage.php', '_top');
}) ...

选项 2:(如果您确实需要验证一些用户详细信息)

您可以检查用户是否已添加到数据库中,然后显示窗口。

类似于:

function getUserStatus(uid) {
return firebase.database().ref('/users/' + uid).once('value').then(function(snapshot) {
if (snapshot.val() != null) {
return snapshot.val().name;
}
return '';
});
}

您可以在您的代码中链接此方法:

firebase.database().ref('users/' + userId).set({
name: name,
email: email,
address: address,
zip: zip,
city: city,
department: department,
membership: membership,
admin: admin,
added: currentTime,
profile_picture : imageUrl
});

getUserStatus(user.uid).then(function(name) {
if (name !== '') {
window.open('frontpage.php', '_top');
}
});

关于javascript - 创建用户,保存数据,然后更改页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45569510/

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