0

I followed a tutorial with regards to building a authentication system for the purpose of learning. I understand the basic behavior and have made changes to fit my needs, however I am running into an error below.

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

I am trying to navigate through the Firebase documentation to find a specific instructions on how to declare custom fields but I cannot find what I am looking for. I am pretty sure it has to deal with;

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

I would really like some help on trying to understand how to properly setup custom data, and I really appreciate the help. Thanks!

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

}

user

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

Console output

 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.eyJuYW1lIjoiTmF2cmFqIEJhaW5zIiwicGljdHVyZSI6Imh0dHBzOi8vbGg0Lmdvb2dsZXVzZXJjb250ZW50LmNvbS8tYnIyUnlqYTRjamsvQUFBQUFBQUFBQUkvQUFBQUFBQUFBUGsvRFc1UGNSSDNSOEkvcGhvdG8uanBnIiwiaXNzIjoiaHR0cHM6Ly9zZWN1cmV0b2tlbi5nb29nbGUuY29tL3BvcnRmb2xpby1uYjg5IiwiYXVkIjoicG9ydGZvbGlvLW5iODkiLCJhdXRoX3RpbWUiOjE1NTIzNDE5MTIsInVzZXJfaWQiOiJ0VWllSU1YVnVoaE8wUTBFMmdNbWlpNHNKQTAzIiwic3ViIjoidFVpZUlNWFZ1aGhPMFEwRTJnTW1paTRzSkEwMyIsImlhdCI6MTU1MjM0MTkxMiwiZXhwIjoxNTUyMzQ1NTEyLCJlbWFpbCI6Im5hdnJhai5iYWluc0BnbWFpbC5jb20iLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiZmlyZWJhc2UiOnsiaWRlbnRpdGllcyI6eyJnb29nbGUu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    __proto__: F

1 Answer 1

1

Good Afternoon! Check out your SetUserData() method. It seems that the user object you're trying to persist doesn't have a value on the admin property. Try console.log('User', result.user); before this.SetUserData(result.user); And please post the console log. Have Fun.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.