Add field separately to firestore document

17,085

Solution 1

Build a DocumentReference to the document you want to update, then use the update() method on the DocumentReference to indicate only the fields to be added or changed. Pass it an object with only properties that match the fields to add or change.

Solution 2

Flutter Example:

var userDoc = Firestore.instance.collection('users').document('replaceIdHere');
userDoc.updateData({'fieldName':'newValue'});
Share:
17,085

Related videos on Youtube

Rashik
Author by

Rashik

Engineering physics student.

Updated on October 09, 2022

Comments

  • Rashik
    Rashik over 1 year

    The following code creates a firestore collection and adds data to it:

    function saveID(sender_psid,complete){
       let data = new Object();
       data.ID = sender_psid;
       data.TASK = complete;
       data.TIME = new Date();
       db.collection('users').add(data);
    }
    

    I want to create another function which adds a field to the document at a different time. I have the following function but am getting the error "TypeError: collectionRef.update is not a function"

    function saveImage(sender_psid,image) {
    
    
      let collectionRef = db.collection('users');
    
    
      collectionRef.update({IMG:image}).then(res => {
      console.log(`Document updated at ${res.updateTime}`);
    });
    }
    
  • Rashik
    Rashik almost 6 years
    Do I build the reference in the second function?
  • Doug Stevenson
    Doug Stevenson almost 6 years
    Build it wherever you need to use it. If you don't need a DocumentReference, then don't bother.
  • Doug Stevenson
    Doug Stevenson almost 6 years
    You built a reference to a collection, not a document within that collection.