Skip to content

Firestore: CollectionReference.withConverter(converter).add(data) invokes converter.toFirestore twice #3742

Description

@hecateball

[REQUIRED] Describe your environment

  • Operating System version: macOS Catalina 10.15.6
  • Browser version: N/A
  • Firebase SDK version: 7.19.1
  • Firebase Product: firestore

[REQUIRED] Describe the problem

firebase.firestore().collection('...').withConverter(converter).add(data) invokes converter.toFirestore twice while firebase.firestore().collection('...').withConverter(converter).doc().set(data) invokes converter.toFirestore only once.
Is this intended behavior?

Although this is not a desirable style of processing, if the toFirestore() process has a side effect on the input value, the second invocation fails (See example below).

Steps to reproduce:

try to run following code please.

Relevant Code:

const firebase = require('firebase')

// Please use your firebase config here
firebase.initializeApp(firebaseConfig)

const document = {
  someArray: [
    { text: '' },
  ],
}

const converter = {
  toFirestore: (modelObject) => {
    console.log('toFirestore', modelObject)
    modelObject.someArray = 'text'
    return { text: modelObject.someArray[0].text }
  },
  fromFirestore: () => {
    throw new ReferenceError()
  },
}

// log 'toFirestore' twice, and then fail second conversion.
firebase.firestore().collection('posts')
  .withConverter(converter)
  .add(document)

// log 'toFirestore' only once and complete as expected.
firebase.firestore().collection('posts')
  .withConverter(converter)
  .doc()
  .set(document)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions