I am trying to add dynamic conditions on firebase query. But somehow not working. I also try chatgpt
but that is also not provide me the best solution. Please help me to fix my issue.
Type ‘CollectionReference’ is not assignable to type ‘Query’.
getListing(): Observable<Listing[]> {
return this.afs
.collection<Listing>('listing', (ref) => {
let query: firebase.firestore.Query =ref;
for (const [key, value] of Object.entries(this.searchFilter)) {
query= query.where(key, "==", value)
}
return ref.orderBy('createdAt', 'asc')
})
.snapshotChanges()
.pipe(
map((actions) =>
actions.map((action) => {
const id = action.payload.doc.id;
const data = action.payload.doc.data() as Listing;
return { id, ...data } as Listing;
})
)
);
}
Any solution appreciated!
2
Answers
After lot of struggle. Finally i am able to solve the issue by my own.
As follow this example in GitHub by defining query as
firebase.firestore.CollectionReference | firebase.firestore.Query
type should be able to resolve the issue.