Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Injectable()
- export class StudentService {
- results$ = new Subject<any>();
- studentsAdded$ = new BehaviorSubject<any>(null);
- private resultArray: any[] = [];
- constructor(private afs: AngularFirestore) { }
- getRecordsBySearch(searchText: any) {
- // console.log(`MD: StudentService -> getRecordsBySearch -> getRecordsBySearch`);
- this.resultArray = [];
- this.results$.next([]);
- if (searchText) {
- // console.log(`MD: StudentService -> getRecordsBySearch -> searchText`, searchText);
- const studentsRef = this.afs.collection('students').ref;
- const fnameQuery = studentsRef.where('fname', '==', searchText);
- const lnameQuery = studentsRef.where('lname', '==', searchText);
- const gradeQuery = studentsRef.where('grade', '==', searchText);
- const queries = [fnameQuery, lnameQuery, gradeQuery];
- this.processQueries(queries);
- }
- }
- getRecordsByEmail(email: string) {
- this.resultArray = [];
- this.results$.next([]);
- const studentsRef = this.afs.collection('students').ref;
- const fe = studentsRef.where('fatherEmail', '==', email);
- const me = studentsRef.where('motherEmail', '==', email);
- const queries = [fe, me];
- this.processQueries(queries);
- }
- public processQueries(queries: Array<any>) {
- queries.forEach(query => {
- query.onSnapshot(querySnapshot => {
- querySnapshot.docs.forEach(queryDocumentSnapshot => {
- const data = queryDocumentSnapshot.data();
- this.resultArray.push(data);
- this.results$.next(this.resultArray);
- });
- // Prevent duplicates in array. Duplication occurs upon a saved edit (assuming there was an actual change).
- const u = this.resultArray.filter((e, i) => this.resultArray.findIndex(a => a.id === e.id) === i);
- this.results$.next(u);
- });
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement