首页 > 解决方案 > Firebase 更新配置文件未在第一次尝试中更新配置文件 || 承诺不处理 || 显示名称

问题描述

我正在使用带有 React Native 的 firebase phone auth 对用户进行身份验证,我想将 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',
  });
}

};

当我运行此功能配置文件未更新时,显示名称仍然为空,但是当第二次用户登录时,它正在添加显示名称。我知道这个问题与第一次没有处理的承诺有关,所以我该如何处理。

标签: javascriptfirebasefirebase-authenticationreact-native-firebase

解决方案


我不确定您的代码中有什么不起作用,但是这个最小的示例对我有用:

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


推荐阅读