Angular Firebase-DocumentReference.set()错误

时间:2019-03-11 18:42:29

标签: angular firebase

我遵循了有关为学习目的构建身份验证系统的教程。我了解基本行为,并进行了更改以满足我的需要,但是在下面遇到了一个错误。

FirebaseError: [code=invalid-argument]: Function DocumentReference.set() called with invalid data. Unsupported field value: undefined (found in field admin)

我试图浏览Firebase文档,以找到有关如何声明自定义字段的特定说明,但找不到所需的内容。我很确定它必须处理;

    SetUserData(user) {
    const userRef: AngularFirestoreDocument<any> = this.afs.doc(`users/${user.uid}`);
    const userData: User = {
      uid: user.uid,
      email: user.email,
      displayName: user.displayName,
      photoURL: user.photoURL,
      emailVerified:user.emailVerified,
      admin: user.admin
    }
    return userRef.set(userData, {
      merge: true
    })
  }

在尝试了解如何正确设置自定义数据方面,我真的很需要帮助,我非常感谢您的帮助。谢谢!

auth.service

import { Injectable, NgZone } from '@angular/core';
import { User } from "../../models/user/user";
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 AuthService {
  userData: any; // Save logged in user data

  constructor(
    public afs: AngularFirestore,   
    public afAuth: AngularFireAuth, 
    public router: Router,  
    public ngZone: NgZone 
  ) {    
    this.afAuth.authState.subscribe(user => {
      if (user) {
        this.userData = user;
        localStorage.setItem('user', JSON.stringify(this.userData));
        JSON.parse(localStorage.getItem('user'));
      } else {
        localStorage.setItem('user', null);
        JSON.parse(localStorage.getItem('user'));
      }
    })
  }

  SignIn(email, password) {
    return this.afAuth.auth.signInWithEmailAndPassword(email, password)
      .then((result) => {
        this.ngZone.run(() => {
          this.router.navigate(['account']);
        });
        this.SetUserData(result.user);
      }).catch((error) => {
        window.alert(error.message)
      })
  }

  get isLoggedIn(): boolean {
    const user = JSON.parse(localStorage.getItem('user'));
    return (user !== null && user.emailVerified !== false) ? true : false;
  }

  GoogleAuth() {
    return this.AuthLogin(new auth.GoogleAuthProvider());
  }

  AuthLogin(provider) {
    return this.afAuth.auth.signInWithPopup(provider)
    .then((result) => {
       this.ngZone.run(() => {
          this.router.navigate(['account']);
        })
      this.SetUserData(result.user);
    }).catch((error) => {
      window.alert(error)
    })
  }

  SetUserData(user) {
    const userRef: AngularFirestoreDocument<any> = this.afs.doc(`users/${user.uid}`);
    const userData: User = {
      uid: user.uid,
      email: user.email,
      displayName: user.displayName,
      photoURL: user.photoURL,
      emailVerified:user.emailVerified,
      admin: user.admin
    }
    return userRef.set(userData, {
      merge: true
    })
  }

  SignOut() {
    return this.afAuth.auth.signOut().then(() => {
      localStorage.removeItem('user');
      this.router.navigate(['login']);
    })
  }

}

用户

export interface User {
  uid: string;
  email: string;
  displayName: string;
  photoURL: string;
  emailVerified: boolean;
  admin: boolean;
}

控制台输出

 User 
    Q {G: Array(0), m: "AIzaSyCMcvKFB-xcIpuc_y23924dDZgYJ1aBfCw", o: "
    G: []
    I: true
    N: [ƒ]
    O: []
    Rb: Q {G: Array(0), m: "AIzaSyCMcvKFB-xcIpuc_y23924dDZgYJ1aBfCw", o: "
    V: ƒ ()
    Va: null
    W: bm {m: false, settings: Rk, app: FirebaseAppImpl, b: Jh, N: Array(0), …}
    X: bm {m: false, settings: Rk, app: FirebaseAppImpl, b: Jh, N: Array(0), …}
xcIpuc_y23924dDZgYJ1aBfCw", m: "[DEFAULT]", h: Array(2), f: true, …}
    b: Jh {b: "AIzaSyCMcvKFB-xcIpuc_y23924dDZgYJ1aBfCw", i: "https://securetoken.googleapis.com/v1/token", l: Ce, f: {…}, g: "https://www.googleapis.com/identitytoolkit/v3/relyingparty/", …}
    ba: Pl {a: "AIzaSyCMcvKFB-xcIpuc_y23924dDZgYJ1aBfCw:[DEFAULT]", b: Kj}

    emailVerified: true
    h: Yk {f: Jh, a: "AEu4IL30a2_ONvQNEZ6vrNX4Ps-F_so4U8lgoJ2Dk3gZcu__35…NsyUKyuirXYpjGTnw0VggX4Gk9CkP3CUVr0woBmgyu3-oEOsI", b: "eyJhbGciOiJSUzI1NiIsImtpZCI6IjkwYmVmMzI2MmVkMzI0Mz…Q5HOt8Pyffn-K3smoXuy31tOcWwubVGgGobX1Bs3NhU8xiJAg", c: 1552345513374}
    i: null
    isAnonymous: false
    ja: undefined
    ka: null
    l: lc {src: Q, a: {…}, b: 3}
    m: "AIzaSyCMcvKFB-xcIpuc_y23924dDZgYJ1aBfCw"
    metadata: cl {a: "1551803509525", b: "1552341792606", lastSignInTime: "Mon, 11 Mar 2019 22:03:12 GMT", creationTime: "Tue, 05 Mar 2019 16:31:49 GMT"}
    o: "[DEFAULT]"
    phoneNumber: null
    photoURL: "https://lh4.googleusercontent.com/-br2Ryja4cjk/AAAAAAAAAAI/AAAAAAAAAPk/DW5PcRH3R8I/photo.jpg"
    providerData: [el]
    qa: false
    ra: "eyJhbGciOiJSUzI1NiIsImtpZCI6IjkwYmVmMzI2MmVkMzI0MzZkNzhlMjdjYWJhYzg3YmIwZWUxZGYwYzIiLCJ0eXAiOiJKV1QifQ.eyJuYW1lIjoiTmF2cmFqIEJhaW5zIiwicGljdHVyZSI6Imh0dHBzOi8vbGg0Lmdvb2dsZXVzZXJjb250ZW50LmNvbS8tYnIyUnlqYTRjamsvQUFBQUFBQUFBQUkvQUFBQUFBQUFBUGsvRFc1UGNSSDNSOEkvcGhvdG8uanBnIiwiaXNzIjoiaHR0cHM6Ly9zZWN1cmV0b2tlbi5nb29nbGUuY29tL3BvcnRmb2xpby1uYjg5IiwiYXVkIjoicG9ydGZvbGlvLW5iODkiLCJhdXRoX3RpbWUiOjE1NTIzNDE5MTIsInVzZXJfaWQiOiJ0VWllSU1YVnVoaE8wUTBFMmdNbWlpNHNKQTAzIiwic3ViIjoidFVpZUlNWFZ1aGhPMFEwRTJnTW1paTRzSkEwMyIsImlhdCI6MTU1MjM0MTkxMiwiZXhwIjoxNTUyMzQ1NTEyLCJlbWFpbCI6Im5hdnJhai5iYWluc0BnbWFpbC5jb20iLCJlbWFpbF92ZXJpZmllZCI6d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OAQ5HOt8Pyffn-K3smoXuy31tOcWwubVGgGobX1Bs3NhU8xiJAg"
    __proto__: F

1 个答案:

答案 0 :(得分:1)

下午好!签出您的SetUserData()方法。似乎您要保留的用户对象的admin属性没有值。在console.log('User', result.user);之前先尝试this.SetUserData(result.user);,然后发布控制台日志。玩得开心。