Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

How to update multiple documents in firebase using react Web API

I am trying to create a web based chat application using Firebase and React, and in this application I am trying to manage if the message is seen or not using a key:value pair in the collection. The structure of collection is like:

chat
    group1
        -MAQ88bAEqaCqzu6RORb
            message: "Hi"
            msgRead: false
            ...
        -MAQ89luju7TEkimtlNd
            message: "Hi"
            msgRead: false
            ...

and so on...

I am trying to update the msgRead value to true in a batch update query like:

const db    = firebase.firestore();
let group1  = db.collection("chat").doc("group1");
let batch   = db.batch();
let response= batch.update(group1, 'msgRead', true);

but it doesn't effect any document at all. Please some one guide me what I am doing wrong here.

Thanks in advance.

Upvotes: 1

Views: 1449

Answers (1)

zkohi
zkohi

Reputation: 2520

See https://firebase.google.com/docs/firestore/manage-data/transactions#batched-writes

You should call batch.commit().

ex.

// Get a new write batch
var batch = db.batch();

// Set the value of 'NYC'
var nycRef = db.collection("cities").doc("NYC");
batch.set(nycRef, {name: "New York City"});

// Update the population of 'SF'
var sfRef = db.collection("cities").doc("SF");
batch.update(sfRef, {"population": 1000000});

// Delete the city 'LA'
var laRef = db.collection("cities").doc("LA");
batch.delete(laRef);

// Commit the batch
batch.commit().then(function () {
    // ...
});

Upvotes: 2

Related Questions