I have an onWrite Cloud Function
exports.testFunction = functions.firestore.document("test/{docId}").onWrite(async (change, context) => {
return await test(change);
});
At the topmost of test i have a variable listener, then i have two conditions that checks whether a document was just created or deleted. Inside the first if block i assign ...onSnapshot(...) to listener then inside the second if block i call listener() to unsubscribe from the GeoQuery.onSnapshot subscription made in the first if block, in other words, when a document is created listener is assigned to ...onSnapshot(...). Subsequently when that same document is deleted, i unsubscribe from the previous onSnapshot subscription
const test = async (change) => {
let listener;
if (!change.before.exists && change.after.exists) {
// document created, subscribe
listener = GeoFirestore.collection(...)
.where("category", "==", ...)
.near({ center: ..., radius: ... })
.onSnapshot(async (snapshot) => {...}, (error) => {...});
}
if (change.before.exists && !change.after.exists) {
// document deleted, unsubscribe
listener();
}
The problem is i am getting an error when the second if block gets triggerred
TypeError: listener is not a function
This Cloud Function is in such a way that the first if block will always get called before the second if block (obviously a document can get deleted only after it is created) and according to the Geofirestore docs
onSnapshot
get onSnapshot(): (onNext: (snapshot: GeoQuerySnapshot) => void, onError?: (error: Error) => void) => () => voidDefined in src/GeoQuery.ts:74
Attaches a listener for GeoQuerySnapshot events. Returns
(onNext: (snapshot: GeoQuerySnapshot) => void, onError?: (error: Error) => void) => () => voidAn unsubscribe function that can be called to cancel the snapshot listener.
I don't see what i seem to be doing wrong here. I need some help