问题
I have the following query:
loadData(settings: any): Observable<any[]> {
const ref = this.db.collection('data').ref
.orderBy('simpleID', 'desc')
.where('isVerified', '==', true)
.limit(5);
// Gender filter
if (settings.includeMales && !settings.includeFemales) {
ref.where('gender', '==', 'm');
} else if (!settings.includeMales && settings.includeFemales) {
ref.where('gender', '==', 'f');
}
return this.db.collection('confessions', ref => ref)
.valueChanges();
I want to apply filters dynamically based on the settings
object.
How can I use the ref variable and pass it to the collect()
method of Angularfire2?
回答1:
Every time you call a where()
or other method, it actually returns a new query. So you just need to make sure to capture the query of each call.
var query = this.db.collection('data').ref
.orderBy('simpleID', 'desc')
.where('isVerified', '==', true)
.limit(5);
if (settings.includeMales && !settings.includeFemales) {
query = query.where('gender', '==', 'm');
} else if (!settings.includeMales && settings.includeFemales) {
query = query.where('gender', '==', 'f');
}
return this.db.collection('confessions', ref => query).valueChanges();
来源:https://stackoverflow.com/questions/51221167/angularfirestore-update-query-dynamically