DocumentReference from QueryDocumentSnapshot in model converter not typed correctly. (original) (raw)

[REQUIRED] Describe your environment

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