In the current code snippet below, I am iterating over a collection of data and updating a field if the email matches. The issue arises when trying to set new values where it crashes. The iteration process itself functions correctly, with afs being AngularFirestore.
onChangeRole(email) {
this.afs.collection("users").get().toPromise().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
if (doc.data().email == email) {
this.afs.collection("users").doc(doc.id).set({
role: 2
})
}
});
});
}
An error is thrown:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'afs' of undefined TypeError: Cannot read property 'afs' of undefined
The variable afs refers to AngularFirestore as shown in the import statement:
import { AngularFirestore, AngularFirestoreCollection , AngularFirestoreDocument} from '@angular/fire/firestore';