Firestore - batch.add is not a function

The documentation for Firestore batch writes lists only set(), update() and delete() as permitted operations.

Is there no way to add an add() operation to the batch? I need a document to be created with an auto-generated id.

25214 次浏览

According to the docs

Behind the scenes, .add(...) and .doc().set(...) are completely equivalent, so you can use whichever is more convenient.

Perhaps this applies to batches as well?

You can do this in two steps:

// Create a ref with auto-generated ID
var newCityRef = db.collection('cities').doc();


// ...


// Add it in the batch
batch.set(newCityRef, { name: 'New York City' });

The .doc() method does not write anything to the network or disk, it just makes a reference with an auto-generated ID you can use later.

Create the reference to the collection in which you are going to add the batch data We loop over the req.body using forEach and set the each data to be added in to the collection using the set method

We commit the data and save the data to the collection using the commit method and on success ,send a success response.

cloud firestore

In my case, using AngularFire2, I had to use the batch.set() method, passing as first parameter the document reference with an ID previously created, and the reference attribute:

import { AngularFirestore } from '@angular/fire/firestore';
...
private afs: AngularFirestore
...
batch.set(
this.afs.collection('estados').doc(this.afs.createId()).ref,
er.getData()
);

For PHP you can try :

$batch = $db->batch();
$newCityRef = $db->collection('cities')->newDocument();
$batch->set($newCityRef , [ 'name'=>'New York City' ]);

Lets assume that you have list of cities and you want to write them in batch.

 final CityList = FirebaseFirestore.instance.collection('cities')
WriteBatch batch = FirebaseFirestore.instance.batch();
for(CityList city in cities) {
final newShoppingItem = ShoppingList.doc();
batch.set(newShoppingItem, {
'name': city.name,
'createdAt': DateTime
.now()
.millisecondsSinceEpoch
});
}
batch.commit();

Sam Stern's answer is the correct way to do it, although if you are using AngularFire, .doc() cannot be used withouth a parameter to generate a new docId (see https://github.com/angular/angularfire/issues/1974).

The AngularFire way of doing this would be:

// Create a ref with auto-generated ID
const id = this.db.createId();
const newCityRef= this.db.collection("cities").doc(id);


// ...


// Add it in the batch
batch.set(newCityRef, { name: 'New York City' });

I'll offer an answer for Firebase 9 in which the syntax differs from Firebase 8.

For Firebase 9, the equivalent of add() is addDoc() as explained at https://firebase.google.com/docs/firestore/manage-data/add-data#web-version-9_6 . It is for when you're not using batch nor transaction. As per the original problem posted, there is no equivalent of addDoc() on batch nor transaction on Firebase 9 either.

I found a way to achieve the equivalent of addDoc() for a batch on Firebase 9 by following the answer https://stackoverflow.com/a/69859144/2848676 as follows:

        const batch = writeBatch(db);
const docADocRef = doc(collection(db, "DocA"));
batch.set(docADocRef, {
fieldA: "This is field of an instance of DocA"
});
const docBDocRef = doc(collection(db, "DocB"));
batch.set(docBDocRef, {
docAID: docADocRef.id
});
batch.commit();

In this example, instances of DocA and DocB are created and DocB receives a pointers to the DocA instance.

This worked for me and it is mentioned in the docs for PHP

 $batch = $db->batch();


# Set the data for NYC
$nycRef = $db->collection('samples/php/cities')->document('NYC');
$batch->set($nycRef, [
'name' => 'New York City'
]);


# Update the population for SF
$sfRef = $db->collection('samples/php/cities')->document('SF');
$batch->update($sfRef, [
['path' => 'population', 'value' => 1000000]
]);


# Delete LA
$laRef = $db->collection('samples/php/cities')->document('LA');
$batch->delete($laRef);


# Commit the batch
$batch->commit();

To create a document with auto-generated ID with firestore batch, you cannot use the addDoc(). You have to use batch.set() with a reference to the document to be created as below

   // Create a transaction to update both the product stock value and add the new stock data
const batch = writeBatch(getFirestore());
const prodRef = doc(getFirestore(), `products/${productId}`);
const stockRef = doc(collection(getFirestore(),`stocks`);
// newDocId = stockRef.id;
batch.set(stockRef, stock, {merge: true}); //create new document with autoId
batch.update(prodRef, {available : increment(quantity), stock: increment(quantity)});
batch.commit()