我正在使用Cloud Firestore和Angular6。我正在尝试根据用户的某些属性(即他们是否教科目)过滤用户的集合。
如何返回过滤器为true还是false?当前,代码返回subjectOffered
,经过一些检查后将其设置为true或false。但是,在应该匹配的情况下,当我在控制台日志中记录该值时,将返回该函数,然后将subjectOffered
的值设置为true。因此,所有用户都被过滤掉。如何返回该值,以便过滤器正常工作?
我有以下代码:
getUserOnSearch(subject: string, level: string, oneToOne: boolean) {
this.userCollection = this.afs.collection<User>('users', ref => {
// console.log(ref);
return ref
});
this.usersOnSearch = this.userCollection.valueChanges();
return this.usersOnSearch.flatMap(user => {
return of(user.filter(value => {
let docID: string = value.user_subjects.id;
let userLevelPriceDocument: AngularFirestoreDocument<UserSubjects>;
userLevelPriceDocument = this.afs.doc<UserSubjects>(`user_subjects/${docID}`);
let subjectOffered: boolean = false;
userLevelPriceDocument.valueChanges().forEach(
docData => {
let subjects: IndividualUserSubject[] = docData.subjects;
subjects.forEach(individualSubject => {
if (individualSubject.name === subject) {
let priceLevel: LevelPrice[] = individualSubject.prices;
priceLevel.forEach(indivLevel => {
if (indivLevel.level == level && indivLevel.price > 0) {
subjectOffered = true;
console.log(subjectOffered);
} else {
subjectOffered = false;
}
})
}
})
}
)
console.log(subjectOffered);
subjectOffered;
})
)
})
}
谢谢!