我在这里链接 promise :
- 首先发送验证电子邮件
- 然后更新用户个人资料信息
- 然后将用户重定向到我应用的仪表板页面
我期望 .then 调用仅在前面的调用完成执行后才会发生。但相反,重定向是在发送验证电子邮件或更新个人资料之前发生的。下面是负责相同功能的代码段:
function authorizeWithFireBase(email,displayName,password,photoURL){
var user = null;
var failText = document.getElementById("signup-invalid-message");
//NULLIFY EMPTY ARGUMENTS
for (var i = 0; i < arguments.length; i++) {
arguments[i] = arguments[i] ? arguments[i] : null;
}
auth.createUserWithEmailAndPassword(email, password)
.then(function () {
user = auth.currentUser;
user.sendEmailVerification();
})
.then(function () {
user.updateProfile({
displayName: displayName,
photoURL: photoURL
});
})
.then(function () {
window.location.replace("/path/to/private/app/dashboard/page");
})
.catch(function(error) {
if(error.code == 'auth/weak-password') {
failText.innerHTML = "Please set a complex password!"
}
else {
failText.innerHTML = error.message;
}
});
console.log('Validation link was sent to ' + email + '.');
}
我尝试切换
.then(function() {...})
到
.then( () => function() {...})
但是 3 .then() 部分都没有被执行。
我做错了什么以及如何链接这些 promise 来执行上述所有 3 个步骤?
最佳答案
您必须从每个 then()
中返回一个 promise 才能链接它们。幸运的是你sendEmailVerification()
和 updateProfile()
两个都返回 promise ,所以这是一个简单的更改:
auth.createUserWithEmailAndPassword(email, password)
.then(function () {
user = auth.currentUser;
return user.sendEmailVerification();
})
.then(function () {
return user.updateProfile({
displayName: displayName,
photoURL: photoURL
});
})
.then(function () {
window.location.replace("/path/to/private/app/dashboard/page");
})
.catch(function(error) {
if(error.code == 'auth/weak-password') {
failText.innerHTML = "Please set a complex password!"
}
else {
failText.innerHTML = error.message;
}
});
关于javascript - Firebase 在发送验证电子邮件之前发布成功注册重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45382691/