I have a problem, here is the code:
const firstName = "rick";
const users = await db.collection('user');
if (firstName != undefined) {
users.where("name", "==", firstName );
}
const snap = users.get();
why this query return me the whole document with all objects? How to do a query based on where
clause that can be null or undefined? Thanks for any help!
CodePudding user response:
You are not reassigning the users
and hence it's fetching whole collection. Try refactoring like this:
let users: firebase.firestore.Query = db.collection('user');
//^ let instead of const
if (firstName) {
users = users.where("name", "==", firstName );
// ^^ updating original 'users' value
}
const snap = users.get();