亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

創建帳戶后 displayName 始終返回 null

創建帳戶后 displayName 始終返回 null

揚帆大魚 2023-04-14 17:17:53
我有一個使用 Firebase 作為后端的 Vue 應用程序。新用戶使用電子郵件和密碼選項注冊。這是 firebase 方法:firebase.auth()          .createUserWithEmailAndPassword(this.user.email, this.user.password)          .then((res) => {            res.user              .updateProfile({                displayName: this.user.username,              })              .then(() => {              });          })          .catch((error) => {            this.error = error.message;            console.log("err", error);          });在我的 main.js 文件中,我有如下所示的 onAuthStateChanged 方法:firebase.auth().onAuthStateChanged((user) => {  if (user) {    console.log("user", user);    console.log("nme", user.displayName);    console.log("email", user.email);    store.dispatch("fetchUser", user);  } else {    store.dispatch("logout");  }當用戶注冊時,當然會觸發此方法。問題是我無法訪問用戶的 displayName 屬性,由于某種原因,當用戶注冊時它總是空的。當我刷新頁面時,它有值,但在注冊它的 null 并將 null 值作為用戶名傳遞給 Vuex 之后。奇怪的是,例如可以立即訪問電子郵件。這是我的控制臺的屏幕截圖:第一部分是“console.log("user", user),然后是其他打印。正如您在用戶對象中看到的那樣,displayName 有一個值,但是當我調用 user.displayName 時,它為空。有人可以向我解釋為什么會這樣嗎?提前致謝!
查看完整描述

1 回答

?
慕森王

TA貢獻1777條經驗 獲得超3個贊

這是因為該updateProfile()方法是異步的,不會觸發通過 設置的監聽器onAuthStateChanged()。

因此,當onAuthStateChanged()在創建用戶(并登錄,因為在創建用戶帳戶之后,用戶也已登錄)后立即觸發偵聽器時, 的值尚未更新displayName。

當方法返回的承諾updateProfile()得到解決時,您可能應該更新 Vuex Store 中的狀態。

以下幾行:

?firebase

? ? .auth()

? ? .createUserWithEmailAndPassword(this.user.email, this.user.password)

? ? .then((res) => {

? ? ? return res.user.updateProfile({

? ? ? ? displayName: this.user.username,

? ? ? });

? ? })

? ? .then(() => {

? ? ? //Update the Vuex Store here with firebase.auth().currentUser

? ? ? console.log(firebase.auth().currentUser.displayName);

? ? })

? ? .catch((error) => {

? ? ? this.error = error.message;

? ? ? console.log('err', error);

? ? });


查看完整回答
反對 回復 2023-04-14
  • 1 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號