Enrique GF
Enrique GF

Reputation: 1295

set in firestore the new displayName updated with Angular

Hola developers being on this app with angular and firebase i already update my display name for user created , but now trying to keep that name in my firestore im having some issues.

Service class where in i access my firstore and my fireauth modules

export class LoginService {

constructor(
    private userLogin: AngularFireAuth,
    private docCreator: AngularFirestore
  ) {}

signUpUser(emailUser: string, passwordUser: string) {
    return new Promise((response, reject) => {
      this.userLogin
        .createUserWithEmailAndPassword(emailUser, passwordUser)
        .then(
          (responseOk) => {
            this.userdata.uid = responseOk.user.uid;
            this.userdata.restData = responseOk.user;

            responseOk.user.updateProfile({
              displayName: 'nameUpdated'
            }).then(() => {
              console.log('displayname setted');
              response(responseOk);
            });

            let userDoc = this.docCreator
              .collection('users')
              .doc(responseOk.user.uid);===variable accessing my collection and doc of user created====

            userDoc.set({
              amountInAccount: 0,
              userName:responseOk.user.displayName(HERE THE ISSUE)
            });===trying to store in my new user created doc the displayname already updated====FAIL!!!
}

thus basically in this service i just try to once the displayName is updated access through a variable initialized in my constructor the FirestoreModule, and then through the path(collection/doc) reaching to the place i want to set that new name i use the method set, and accessing the key created in firestore i updated with my new displayName, but doesnt work. Any idea about how could i improve this .Thanks in advance!!!

Upvotes: 1

Views: 745

Answers (1)

Radik
Radik

Reputation: 2795

when you need call many method who returns Promise you can do async/await example 1 and example 2

    async createUser(emailUser: string, passwordUser: string) {

        const responseOk = await this.auth
            .createUserWithEmailAndPassword(emailUser, passwordUser);

        this.userdata.uid = responseOk.user.uid;
        this.userdata.restData = responseOk.user;

        await responseOk.user.updateProfile({
          displayName: displayName
        });

        console.log('displayname setted', responseOk.user.displayName);

        let userDoc = this.docCreator
          .collection('users')
          .doc(responseOk.user.uid);

        await userDoc.set({
          amountInAccount: 0,
          userName: responseOk.user.displayName
        });

        console.log('userDoc setted');
        return responseOk;
    }

Upvotes: 3

Related Questions