E471 DocumentReference from QueryDocumentSnapshot in model converter not typed correctly. · Issue #4278 · firebase/firebase-js-sdk · GitHub
[go: up one dir, main page]

Skip to content

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

@letsgamedotdev

Description

@letsgamedotdev

[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;
  },

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0