簡體   English   中英

Firebase 更新配置文件未在第一次嘗試中更新配置文件 || Promise 不處理 || 顯示名稱

[英]Firebase Update Profile Not Updating profile in First Attempt || Promise NoT Handling || displayName

我正在使用帶有 React native 的 firebase 手機身份驗證用戶進行身份驗證,我想將 displayName 添加到當前用戶,但在創建帳戶期間無法添加,因此在閱讀文檔后,我正在添加這樣的顯示名稱。

export const otpCustomer = user => async dispatch => {
dispatch({type: OTP_USER_LOADING});
try {
  const verification = 'unverified';
  const date = Date.now()
  const AccountStatus = 'open'
  const Devicetoken = {os: 'default', token: 'not defined'};
  const {phone, name , userId } = user;
  const type = 'customer' 

  let usp =   await auth().currentUser.updateProfile({
    displayName:'customer'
  });
 
  console.log('usppppp',usp)
  const req = await fetch(
      `https://url.firebaseio.com/customers/${userId}.json`,
      {
        method: 'put',
        headers: {
          ContentType: 'application/json',
        },
        body: JSON.stringify({name,phone,verification,
          Devicetoken,date,AccountStatus,customerId:userId,type}),
      },
    );
  const res = await req.json()
  if (res.error) {
    dispatch({
      type: OTP_USER_FAIL,
      payload: 'error',
    });
  } else {
   
    dispatch({
      type: OTP_USER_SUCCESS,
      payload: res,
    });
  }

} catch (err) {
  console.log('err',err)
  dispatch({
    type: OTP_USER_FAIL,
    payload: 'err',
  });
}

};

當我運行這個 function 配置文件沒有更新時,顯示名稱仍然是 Null,但是當第二次用戶登錄時,它正在添加顯示名稱。 我知道這個與 promise 相關的問題不是第一次處理,所以我該如何處理。

我不確定您的代碼中有什么不起作用,但是這個最小的示例對我有用:

firebase.auth().signInAnonymously().then(function() {
  firebase.auth().currentUser.updateProfile({ 
    displayName:'customer '+new Date() 
  }).then(function() {
    console.log(firebase.auth().currentUser.displayName);
  });
});

每次我運行代碼時,顯示名稱都會立即更新為當前日期/時間。

https://jsbin.com/nabaxal/edit?js,console

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM