Firestore - batch.add is not a function

前端 未结 6 1099
滥情空心
滥情空心 2020-12-24 10:31

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

Is there no way

相关标签:
6条回答
  • 2020-12-24 11:07

    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?

    0 讨论(0)
  • 2020-12-24 11:17

    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()
      );
    
    0 讨论(0)
  • 2020-12-24 11:19

    For PHP you can try :

    $batch = $db->batch();
    $newCityRef = $db->collection('cities')->newDocument();
    $batch->set($newCityRef , [ 'name'=>'New York City' ]);
    
    0 讨论(0)
  • 2020-12-24 11:19

    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

    0 讨论(0)
  • 2020-12-24 11:27

    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();
    
    0 讨论(0)
  • 2020-12-24 11:33

    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.

    0 讨论(0)
提交回复
热议问题