【问题标题】:How to use updateProfile in firebase / Angular如何在 firebase / Angular 中使用 updateProfile
【发布时间】:2020-08-27 14:06:54
【问题描述】:

我想在 firebase 中更新用户的 displayName 和/或照片 URL。我正在使用角度。我有一个处理身份验证内容的服务,它适用于登录/注册/等。我创建了另一个服务来处理用户数据(UserService)。但是,当我尝试调用 updateProfile 函数时,出现以下编译错误。 docs 显示通过用户对象调用的函数,但它没有解释该用户对象是什么。

错误

error TS2339: Property 'updateProfile' does not exist on type 'AngularFireAuth'

我已经尝试了多种方法 - 包括下面的一种方法,它会产生上述错误,并使用 currentUser 函数创建一个用户对象,然后尝试在该对象上调用该方法。似乎没有任何效果。

import { Injectable, NgZone } from '@angular/core';
import { User } from "app/main/services/users";
import { auth } from 'firebase/app';
import { AngularFireAuth } from "@angular/fire/auth";
import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/firestore';
import { Router } from "@angular/router";


@Injectable({
  providedIn: 'root'
})
export class UserService {

  constructor(
        public afs: AngularFirestore,   // Inject Firestore service
        public afAuth: AngularFireAuth, // Inject Firebase auth service
        public router: Router,
        public ngZone: NgZone // NgZone service to remove outside scope warning
    ) { }


    /**
     * Update the image or username
     */
    UpdateProfile( displayName ) {
        console.log('In Update profile');
        return this.afAuth.updateProfile({
            displayName: displayName,
            photoURL: "https://example.com/jane-q-user/profile.jpg"

        }).then((result) => {
              console.log('Did the update profile');

        }, function(error){

        });
    }

 }

【问题讨论】:

    标签: javascript angular firebase firebase-authentication angularfire2


    【解决方案1】:

    AngularFireAuth.currentUser 是一个 async 属性。 您需要添加等待

    例如 -

    async UpdateProfile(displayName: string) {
        const profile = {
            displayName: displayName,
            photoURL: "https://example.com/jane-q-user/profile.jpg"
        }
        return (await this.afAuth.currentUser).updateProfile(profile);
    }
    

    【讨论】:

      【解决方案2】:

      尝试以下方法:

       return this.afAuth.currentUser.updateProfile({
                  displayName: displayName,
                  photoURL: "https://example.com/jane-q-user/profile.jpg"
      
              })
      

      AngularFireAuth 承诺代理一个初始化的firebase.auth.Auth 实例,允许您登录、注销用户等。

      currentUserfirebase.auth.Auth 内部的一个属性,它返回一个User 的实例。

      【讨论】:

      • 这是我尝试过的众多事情之一。我在函数中收到错误消息,指出“Promise”类型上不存在属性“updateProfile”。然后它会询问“您是否忘记使用'await'?”
      • this.afAuth.currentUser.then((result) => { result.updateProfile()});你可以试试这个
      • 好的。所以编译没有错误,但我还没有运行它。 currentUser 方法是否返回一个承诺以获取当前用户,然后在返回承诺时执行命令?为什么文档没有反映这一点?
      • 您正在使用 angularfire github.com/angular/angularfire/blob/master/docs/auth/… 检查 angularfire 文档...如果您通常使用纯 js,您只需执行 var user = firebase.auth().currentUser; 无论如何尝试并告诉我
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      • 2021-07-13
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 2021-02-16
      相关资源
      最近更新 更多