I am using the new Angular Firebase v.7 with Angular and I am getting an error: Cannot use 'in' operator to search for '_delegate' in users/1QAvZYg6aqe0GhA13tmVAINa
.
There is a similar question ( Firebase Error: TypeError: Cannot use 'in' operator to search for '_delegate' in undefined ) but it is unanswered and I have been trying to little avail to find one myself. Perhaps I am overlooking something very simple.
The code
The service responsible for creating the document is in data.service.ts
as looks like this:
import { Injectable } from '@angular/core';
import {
collection, deleteDoc, doc, DocumentSnapshot, Firestore, onSnapshot,
query, QuerySnapshot, setDoc, Timestamp as fTimestamp, Unsubscribe
} from '@angular/fire/firestore';
import { firstValueFrom, lastValueFrom, Observable, Subject } from 'rxjs';
import { take } from 'rxjs/operators';
export interface FirestoreExtDoc<T> {
data: Observable<T>;
unsubscribe: Unsubscribe;
}
export interface FirestoreExtCol<T> {
data: Observable<T[]>;
unsubscribe: Unsubscribe;
}
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private db: Firestore) { }
upsert<DocumentData>(ref: string, data: any): Promise<void> {
const docRef = doc(this.db, ref);
const timestamp = fTimestamp.now();
const newData = {
...data,
updatedAt: timestamp,
createdAt: timestamp,
};
const updatedData = {
...data,
updatedAt: timestamp,
};
const snapshot = lastValueFrom(this.getDoc<DocumentData>(ref).data.pipe(take(1)));
return snapshot.then(
snap => (snap as any).exists ?
setDoc(docRef, updatedData, { merge: true }) :
setDoc(docRef, newData, { merge: true })
);
};
}
In my auth.service.ts
I import the above data service and call the upsert
method like this:
import { Injectable } from '@angular/core';
import {
Auth,
signInWithEmailAndPassword,
GoogleAuthProvider,
authState,
createUserWithEmailAndPassword,
updateProfile,
UserInfo,
signInWithPopup,
sendPasswordResetEmail
} from '@angular/fire/auth';
import {
doc,
collection,
collectionGroup,
setDoc,
updateDoc,
deleteDoc,
docSnapshots,
docData,
getDoc
} from '@angular/fire/firestore';
import { User } from '@core/interfaces/user';
import { concatMap, from, Observable, of, switchMap } from 'rxjs';
import { DataService } from './data.service';
@Injectable({
providedIn: 'root',
})
export class AuthService {
currentUser$ = authState(this.auth);
constructor(
private auth: Auth,
private dataService: DataService
) {}
signUp(name: string, email: string, password: string): Observable<any> {
return from(
createUserWithEmailAndPassword(this.auth, email, password)
).pipe(switchMap(({ user }) => updateProfile(user, { displayName: name }).then((data) => { this.dataService.upsert(`users/${user.uid}`, data) }) ));
}
}