Javascript 如何制作;onAuthStateChanged“;“等待”;currentUser.updateProfile";结束?
在我的Vue应用程序中注册用户时遇到问题。。。当用户注册时,我想启动Javascript 如何制作;onAuthStateChanged“;“等待”;currentUser.updateProfile";结束?,javascript,firebase,vue.js,firebase-authentication,Javascript,Firebase,Vue.js,Firebase Authentication,在我的Vue应用程序中注册用户时遇到问题。。。当用户注册时,我想启动updateProfile以添加更多用户数据,然后在my main.js的onAuthStateChanged中,将所有用户数据保存在vuex中以在我的应用程序中使用,但onAuthStateChanged在updateProfile之前执行,因此,在vuex中,displayName和photoURL未定义 如何使AuthStateChanged上的等待更新配置文件完成 我在Register.vue组件中具有以下功能: reg
updateProfile
以添加更多用户数据,然后在my main.js的onAuthStateChanged
中,将所有用户数据保存在vuex中以在我的应用程序中使用,但onAuthStateChanged
在updateProfile
之前执行,因此,在vuex中,displayName和photoURL未定义
如何使AuthStateChanged上的等待更新配置文件
完成
我在Register.vue组件中具有以下功能:
register() {
firebase
.auth()
.createUserWithEmailAndPassword(this.email, this.password)
.then(user => {
firebase
.auth()
.currentUser.updateProfile({
displayName: this.displayName,
photoURL: this.photoURL
})
.then(() => {
db.collection("users")
.doc(user.user.uid)
.set({
email: this.email,
displayName: this.displayName,
realName: this.realName,
photoURL: this.photoURL
})
.then(() => {
console.log(user);
this.$router.replace("/");
})
.catch(err => {
this.errorMessage = err.message;
});
})
.catch(err => {
this.errorMessage = err.message;
});
})
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode == "auth/weak-password") {
alert("The password is too weak.");
} else {
alert(errorMessage);
}
console.log(error);
});
}
}
在我的main.js中有:
let app = "";
firebase.auth().onAuthStateChanged((user) => {
store.dispatch("fetchUser", user);
if (!app) {
new Vue({
router,
store,
render: (h) => h(App),
}).$mount("#app");
}
});
您得到未定义的的原因可能是因为您没有从register
方法返回异步函数
你现在有这个
寄存器(){
firebase//未定义,因为您不返回任何内容
.auth()
.createUserWithEmailAndPassword(this.email,this.password)
。然后(用户=>{
火基
.auth()
.currentUser.updateProfile({
....
})
.then(()=>{//未定义,因为在上一个then块中没有返回
数据库收集(“用户”)
.doc(user.user.uid)
.设置({
......
})
.then(()=>{//未定义,因为在上一个then块中未返回
....
})
.catch(错误=>{
...
});
})
.catch(错误=>{
....
});
})
.catch(函数(错误){
....
});
}
}
您得到未定义的的原因可能是因为您没有从寄存器
方法返回异步函数
你现在有这个
寄存器(){
firebase//未定义,因为您不返回任何内容
.auth()
.createUserWithEmailAndPassword(this.email,this.password)
。然后(用户=>{
火基
.auth()
.currentUser.updateProfile({
....
})
.then(()=>{//未定义,因为在上一个then块中没有返回
数据库收集(“用户”)
.doc(user.user.uid)
.设置({
......
})
.then(()=>{//未定义,因为在上一个then块中未返回
....
})
.catch(错误=>{
...
});
})
.catch(错误=>{
....
});
})
.catch(函数(错误){
....
});
}
}
您是否尝试过使方法异步并将wait放在调用前面?相同的结果。。。你能提供一个例子吗?你有没有尝试过使这个方法异步,并把wait放在调用前面?同样的结果。。。你能举个例子吗?我已经知道为什么我没有定义,不是因为这个。。。这是因为在创建用户时,onAuthStateChanged
会在updateProfile
有机会更新displayName
和photoURL
之前调度用户数据。不管怎样,我尝试了你的建议,结果是一样的。嗨,我能想到的唯一一件事是有一些变量,你可以使用eventBus发送到main.js。在您的检查中,您可以在分派用户之前查看该变量。我会更新我的答案让你看。我已经更新了我的答案,@ObedMonsalve。我暗自怀疑,由于数据
属性是在vue实例中初始化的,因此它在整个应用程序中都是可用的,并且是被动的。我觉得您可以直接在组件中更改isRegistrationRegigeringAuthStateChange
的值,而不是使用eventBus发出一个变量,从而导致数据属性发生更改。尝试用this.isRegistrationContriggeringAuthStateChange=true | false
替换事件总线发射。它应该可以工作。我尝试了两种方法,使用原始的isregistrationriggeringauthstatechange
事件和this.isregistrationriggeringauthstatechange=true
,但都得到了这个结果。还尝试将main.js的数据更改为类似组件的函数,结果相同。或者,如果所有这些都失败,您是否可以将更新配置文件位移动到新方法?它必须在注册位吗?我是fire base的新手,我想当配置文件更新时,onAuthStateChanged
会被触发。我已经知道为什么我没有定义,不是因为这个。。。这是因为在创建用户时,onAuthStateChanged
会在updateProfile
有机会更新displayName
和photoURL
之前调度用户数据。不管怎样,我尝试了你的建议,结果是一样的。嗨,我能想到的唯一一件事是有一些变量,你可以使用eventBus发送到main.js。在您的检查中,您可以在分派用户之前查看该变量。我会更新我的答案让你看。我已经更新了我的答案,@ObedMonsalve。我暗自怀疑,由于数据
属性是在vue实例中初始化的,因此它在整个应用程序中都是可用的,并且是被动的。我觉得您可以直接在组件中更改isRegistrationRegigeringAuthStateChange
的值,而不是使用eventBus发出一个变量,从而导致数据属性发生更改。尝试用this.isRegistrationContriggeringAuthStateChange=true | false
替换事件总线发射。应该行得通。我尝试了两种方法,原来的isRegistrationRegigingAuthStateChange