问题
I have this function, but I´m having this error with the "where" statement.
getUser() {
this.db.collection("users").where("name", "==", "Maria")
}
ERROR: Property 'where' does not exist on type 'AngularFirestoreCollection'.ts(2339)
回答1:
If you want to apply querying in firebase collection. You have to pass queryFn as a second argument to collection method.
Try this:
this.db.collection("users",(ref) => {
let query : firebase.firestore.CollectionReference | firebase.firestore.Query = ref;
query = query.where(`name`, `==`, 'Maria');
return query;
} ).valueChanges();
回答2:
if you're using AngularFire2 you can do it like this:
getUser() {
const query = this.db.collection('users', ref => ref.where('name', '==', 'Maria'); //Remember that the casing matters, Maria !== maria.
// From here you can do whatever you need, use as Promise or Observable:
// Promise Based
query
.then(data => {
return data.map(snaps => {
return snaps;
})
})
.catch((err) => console.log(err));
.finally(() => console.log('completed');
}
If it's plain Firebase SDK
getUser() {
this.db.collection('users').where('name', '==', 'Maria'); //Remember that the casing matters, Maria !== maria.
}
来源:https://stackoverflow.com/questions/61181944/error-with-query-from-firebase-in-the-where-statement