在AngularFirestoreCollection <t>或Observable中转换CollectionReference?

时间:2018-07-03 16:09:47

标签: angular typescript firebase google-cloud-firestore angularfire2

我的method返回了2018-01-22。 但是,在我将其转换为2018-01-23以使用数组之前,此方法曾返回一个2018-01-22

CollectionReference

在使用这种方式之前。努力工作以获取所有数据。

AngularFirestoreCollection<myObj>

现在,我使用了可选参数。

Observable<myObj[]>

如何将humanCol : AngularFirestoreCollection<Human>; humanObersArray: Observable<Human[]>; constructor(private db: AngularFirestore) { } 转换为buildHuman(): Observable<Human[]> { this.humanCol = this.db.collection('path'); return this.humanObersArray = this.humanCol.valueChanges(); } buildHuman(param1: string, param2?: string, param3?: string...){ this.db.collection('path', ref => { let retVal = ref as any; if (param1 != null) { retVal = retVal.where('param1', '==', param1) } if (param2 != null) { retVal = retVal.where('param2', '==', param2) } if (param3 != null) { retVal = retVal.where('param3', '==', param3) } ... return retVal; //this return a CollectionReference }); }

1 个答案:

答案 0 :(得分:1)

您的函数buildHuman不返回任何内容。只需返回this.db.collection电话,您就会收到一个AngularFirestoreCollection。或像以前一样添加呼叫valueChanges以获得observable

buildHuman(param1: string, param2?: string, param3?: string): AngularFirestoreCollection<any> {
    return this.afs.collection('users', ref => {
        let retVal = ref as any;
        if (param1 != null) { retVal = retVal.where('param1', '==', param1); }
        if (param2 != null) { retVal = retVal.where('param2', '==', param2); }
        if (param3 != null) { retVal = retVal.where('param3', '==', param2); }
        return retVal;
    });
}

OR

buildHuman(param1: string, param2?: string, param3?: string): Observable<any> {
    return this.afs.collection('users', ref => {
        let retVal = ref as any;
        if (param1 != null) { retVal = retVal.where('param1', '==', param1); }
        if (param2 != null) { retVal = retVal.where('param2', '==', param2); }
        if (param3 != null) { retVal = retVal.where('param3', '==', param2); }
        return retVal;
    })
    .valueChanges();
}