I recently asked this Question, which had a lot of information in it but I dont think I was asking the right question. Google Firebase Angular Firestore switchMap - Firebase Error DocumentReference.set().
My understanding of AngularFireAuth is that you use it to create a User object with a bunch of values, most of which are immutable. If I would like to add a custom value, I need to do that via Google Firestore.
I have created the following firestore entry with this information in it: .
I've also created a User Model:
export interface User {
uid: string;
email: string;
displayName?: string;
role: string;
thursdayCampaign: Boolean;
menagerieCoast: Boolean;
}
My understanding is that the following code should go to the built-in AngularFireAuth User object to get the uid, and then I should use switchMap to redirect to my firebase store. The problem I'm getting is that when I sign in, this.updateUserData recieves the proper credentials, and then tries to update the AngularFireAuth User Object with the data constant that I create there. This is causing the function to throw an error, as userRef.set(data, { merge: true}) is attempting to merge role, thursdayCampaign, and menagerieCoast, into the AngularFireAuth User object, when those values dont exist on that object.
My question is why is my reference not redirecting. Do I need to call this.user$ somewhere?
The code in question is below. I hope I'm asking this in a better way than my previous question.
import { Injectable } from "@angular/core";
import { Router } from '@angular/router';
import { auth } from 'firebase/app';
import {
AngularFirestore,
AngularFirestoreDocument
} from '@angular/fire/firestore'
import { Observable, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { User } from './user1.model';
import * as firebase from 'firebase';
import { AngularFireAuth } from '@angular/fire/auth';
@Injectable({
providedIn: 'root'
})
export class AuthService {
user$: Observable<User[]>;
constructor(private afAuth: AngularFireAuth,
private afs: AngularFirestore,
private router: Router) {
//This is how we're getting into the firestoreDB
this.user$ = this.afAuth.authState.pipe(
switchMap(user => {
if (user){
return this.afs.doc<User>(`/users/${user.uid}`).valueChanges();
} else {
return of(null)
}
})
)
} //end constructor
public updateUserData(user){
//sets user data to firestore on login
const userRef: AngularFirestoreDocument<User> = this.afs.doc(`users/${user.uid}`)
console.log("userRef", userRef)
const data: User = {
uid: user.uid,
email: user.email,
displayName: user.displayName,
role: user.role,
thursdayCampaign: user.thursdayCampaign,
menagerieCoast: user.menagerieCoast
}
console.log("data", data)
userRef.set(data, { merge: true})
}
async emailSignin(value){
//const provider = new auth.EmailAuthProvider();
const credential = await this.afAuth.signInWithEmailAndPassword(value.email, value.password)
return this.updateUserData(credential.user),
this.router.navigate(['/home'])
}
async googleSignin(){
const provider = new auth.GoogleAuthProvider();
const credential = await this.afAuth.signInWithPopup(provider);
return this.updateUserData(credential.user)
}
Error when calling emailSignin:
ERROR Error: Uncaught (in promise): FirebaseError: [code=invalid-argument]: Function DocumentReference.set() called with invalid data. Unsupported field value: undefined (found in field role) FirebaseError: Function DocumentReference.set() called with invalid data. Unsupported field value: undefined (found in field role)
user.role
since the last time you asked this question? The error message is still telling you that it's undefined. – Doug Stevenson