【问题标题】:set in firestore the new displayName updated with Angular在 Firestore 中设置使用 Angular 更新的新 displayName
【发布时间】:2020-09-07 01:12:34
【问题描述】:

Hola 开发人员正在使用 angular 和 firebase 开发此应用程序,我已经更新了用户创建的显示名称,但现在尝试将该名称保留在我的 firestore 中,我遇到了一些问题。

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!!!
}

因此,基本上在这个服务中,我只是尝试通过在我的构造函数 FirestoreModule 中初始化的变量访问显示名称,然后通过路径(集合/文档)到达我想要设置新名称的地方使用方法集,并访问在 firestore i 中创建的密钥 用我的新 displayName 更新,但不起作用。 关于如何改进这一点的任何想法。提前谢谢!!!

【问题讨论】:

    标签: angular firebase google-cloud-firestore


    【解决方案1】:

    当您需要调用许多返回 Promise 的方法时,您可以执行 async/await example 1example 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;
        }
    

    【讨论】:

    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多