简体   繁体   中英

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

I am authenticating user with firebase phone auth with React native, I want to Add displayName to Current user but I can't add during account creation, so after reading Docs I am adding display Name like this.

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',
  });
}

};

when I run this function Profile is not being update, display Name still Null, But when second time user logged in then it's adding display Name. I know this problem related to promise that is not being handle first time, so how I can Handle this.

I'm not sure what isn't working in your code, but this minimal example is working for me:

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

Each time I run the code, the display name is immediately updated with the current date/time.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM