mikkopaderes/ember-cloud-firestore-adapter

Realtime Listener Error: Cannot destructure property 'unsubscribe' of 'this.queryListeners[queryId]' as it is undefined

ceit-each opened this issue · 2 comments

Coming across the issue shown in the images below quite regularly. When it happens, it removes the listener from the affected query.

In our case, it occurs when a cloud function updates data which is being queried in app with isRealtime = true. But I can recreate by simply writing any data to multiple docs in the query via a script.

Screenshot 2022-11-07 at 08 05 23

Screenshot 2022-11-07 at 08 05 49

The Query:

this.store.query('collectionName', {
  isRealtime: true,
  filter: (ref) => query(ref, where('field1', '==', 'value'), orderBy('field2')),
});

The node script that triggers the error:

let documents = await db.collection('collectionName').where('field1', '==', 'value').limit(2).get();
let promises = [];
documents.forEach(document => {
  promises.push(
    document.ref.update({
      dummy: new Date()
    })
  );
});
await Promise.all(promises);

And interestingly if the script is changed to await each update, the error doesn't seem to occur:

for (let document of documents.docs) {
  await document.ref.update({
    dummy: new Date()
  });
}

Expected behavior
For there to be no error and the listener to not be removed.

Tooling versions (please complete the following information):

  • Ember: 4.7.0
  • Ember Data: 4.7.1
  • Ember Cloud Firestore Adapter: 2.3.0
  • Firebase: 9.6.8

I can imagine how this can be an error and can be caused by having multiple documents updated with very little interval. The for-of loop doesn't trigger the issue most likely because it waits for the .update() to finish before moving on to the next loop unlike your .forEach() example. With that said though, this isn't something I can replicate locally because I think it's a race condition issue, but I think I can still provide a fix for this.

However, if you can, you might to batch your writes if it's possible for your use-case.

Nice one, thanks for looking at this!
No error for me with the fix.