0

There are many similar questions like this, this, and this.

However, none of the solutions worked.

How can you increment a nested Firebase property with the FieldValue.increment function?

This code doesn't have any effect.

    // Set query.
    let query = firebase.firestore()
                            .collection('foobar')
                            .where('id', '==', userId);

    // Run query.
    try {
        const querySnapshot = await query.get();
        return querySnapshot.docs[0].ref.update({
            [`test.${serviceId}.amount`]: firebase.firestore().FieldValue.increment(1)
        });

    } catch(e) {
        console.log('Error: ', e);
    }
Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • According to this [SO question](https://stackoverflow.com/questions/61189443/firebase-firestore-increment-fieldvalue-does-not-increment) you need to have a value existing in the firestore that so it has something to increment. – marian.vladoi Jan 04 '21 at 14:33

1 Answers1

1

Found the problem.

Wrong: firebase.firestore().FieldValue.increment(1)

Right: firebase.firestore.FieldValue.increment(1).

The correct code is not a method call against firestore.

Crashalot
  • 33,605
  • 61
  • 269
  • 439