I've been diving into a chat project using Angular, and Firestore has given me a bit of trouble. Trying to get the hang of typescript while working with it.
Within app.module.ts, kicking things off with:
import { provideFirebaseApp, getApp, initializeApp } from '@angular/fire/app';
import { getFirestore, provideFirestore } from '@angular/fire/firestore'
imports: [
provideFirebaseApp(() => initializeApp(environment.firebase)),
provideFirestore(() => getFirestore()),
],
Created a FirestoreService for data retrieval and updates.
Updates on array in documents are smooth sailing, but querying Firestore has me puzzled - always ending up with undefined results.
import { AngularFireAuth } from '@angular/fire/compat/auth';
import {
Firestore,
getFirestore,
provideFirestore,
collectionData,
collection,
doc,
getDoc,
docData,
query,
setDoc,
updateDoc,
addDoc,
firestoreInstance$,
DocumentData,
arrayRemove,
} from '@angular/fire/firestore';
// More code followed by a rejection method...
async rejectFriendRequest(rejectedUser: string, currentUser: string){
const recipientRef = doc(this.firestore, "users", JSON.stringify(currentUser));
await updateDoc(recipientRef, {
invitationsFrom: arrayRemove(rejectedUser),
rejectedInvitation: arrayUnion(rejectedUser)
});
}
The rejection method is doing its job updating arrays in specific documents. However, reading documents from Firestore has hit a roadblock, unsure about which library to use next. The initial idea was to use email addresses as document IDs, but faced issues when needing to update an ID containing a ".".
One of my attempts involved this snippet:
async getUser(id: string) {
var usersRef = this.aF.collection("users").doc(id);
var colRef = collection(this.firestore, "users")
var docreference = await doc(colRef, id);
docData(docreference).subscribe( (result) => {
console.log(result);
})
}