Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DocumentReference from QueryDocumentSnapshot in model converter not typed correctly. #4278

Closed
letsgamedotdev opened this issue Jan 12, 2021 · 6 comments · Fixed by #4284
Closed
Assignees

Comments

@letsgamedotdev
Copy link

letsgamedotdev commented Jan 12, 2021

[REQUIRED] Describe your environment

  • Operating System version: Windows
  • Browser version: Chrome (87.0.4280.141)
  • Firebase SDK version: 9.1.0
  • Firebase Product: firestore

[REQUIRED] Describe the problem

When trying to save the DocumentReference from the QueryDocumentSnapshot in the ModelConverter.fromFirestore function. The resulting document reference object is missing several fields.

Steps to reproduce:

See code below:

Relevant Code:

interface MyModel {
  ref: firebase.firestore.DocumentReference;
}

const modelConverter = {
  toFirestore: function (model: MyModel) {
    return _.omit(model, "ref");
  },
  fromFirestore: function (
    snapshot: firebase.firestore.QueryDocumentSnapshot
  ): MyModel {
    const model = snapshot.data();
    // Some kind of validation, etc.
    model.ref = snapshot.ref;
    return model as MyModel;
  },
};

const modelDocumentRef = firebase.firestore()
  .collection("/models")
  .doc("some_id")
  .withConverter(modelConverter);

  modelDocumentRef.onSnapshot((snapshot) => {
  const model = snapshot.data();
  const modelSubcollectionRef = model?.ref.collection("/subcollection");
});

Runtime output:

Uncaught (in promise) TypeError: model.collection is not a function

Instead I have to do:

  fromFirestore: function (
    snapshot: firebase.firestore.QueryDocumentSnapshot
  ): MyModel {
    const model = snapshot.data();
    // Some kind of validation, etc.
>   model.ref = firebase.firestore().doc(snapshot.ref.path);
    return model as MyModel;
  },

@google-oss-bot
Copy link
Contributor

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@dconeybe
Copy link
Contributor

This bug looks eerily similar to #4125, which was fixed by #4136 in 8.1.2 (December 3, 2020). I'll investigate and reply back.

@dconeybe
Copy link
Contributor

I've confirmed this issue and it is indeed a bug. I'm working on a fix. In the meantime, your workaround makes perfect sense.

@letsgamedotdev
Copy link
Author

I've confirmed this issue and it is indeed a bug. I'm working on a fix. In the meantime, your workaround makes perfect sense.

This also happens with admin.firestore by the way. Not sure if the projects are related or if I should make another issue ticket.

@dconeybe
Copy link
Contributor

If this bug is also happening with the admin sdk then it can't possibly be the same root cause. Please open a new ticket for it.

@dconeybe
Copy link
Contributor

FYI the fix for this bug was released in version 8.2.4 on January 21, 2021: https://firebase.google.com/support/release-notes/js#version_824_-_january_21_2021

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
3 participants