Open Daeon97 opened 2 years ago
Creating a listener would not make sense for most Cloud Functions. I assume it does return a Snapshot once but I do not think that Cloud Functions would return a method.
In other words if you create a query without GeoFirestore
I would be very surprised if it did return a method to detach. If it does then I have overlooked it and it is 'missing', but I really can't see a realistic use case.
The setup on Stackoverfow currently implies that what you are trying to achieve is; When a new document is created, the cloud function should listen forever for documents that are created near it, until at some point in time that source document is deleted.
Besides the technical limitations (cloud functions have a execution time), that would be very expensive to run as all documents changes would create a new instance that wait until it is deleted. I think you might want to redesign the triggers and add one that observes if a 'category' is changed. Then you might want to use use .collectionGroup('user_documents')
to query what nested documents within your structure need to be updated.
So normally you would:
listen to new documents (e.g /users/{userId}/documents)
.collection('...').where().near().get()
// get relevant/near categorieslisten to changed categories (e.g /categories)
.collectionGroup('documents').where('category', '==', '....')near().get()
// searches within all collections named 'documents' (e.g /users/[someUserId]/documents & /users/[otherUserId]/documents)
Hello Michael π I am having issues unsubscribing from a GeoQuery.onSnapshot in an
onWrite
Cloud Function.At the topmost of the function body i have a variable
listener
let listener;
Then i have two conditions that checks whether a document was just created or deletedif(!change.before.exists && change.after.exists){ /* checks if document was just created */ }
andif(change.before.exists && !change.after.exists){ /* checks if document was just deleted */ }
Inside the first
if
block i assign...onSnapshot(...)
tolistener
listener = ...onSnapshot(...);
Then inside the secondif
block i calllistener()
to unsubscribe from theGeoQuery.onSnapshot
subscription made in the first if block as stated in your API docs, in other words, when a document is created thelistener
is assigned to...onSnapshot(...)
. Subsequently when that same document is deleted, i try to unsubscribe from the previousonSnapshot
subscriptionThe problem is i am getting an error
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). I don't see what i seem to be doing wrong here. I need some help
I have also posted a more elaborate description of the issue i am facing on Stackoverflow here https://stackoverflow.com/questions/73803324/unsubscribe-from-geoquery-onsnapshot-on-document-deletion