Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a field to Firestore?

I have a field called jobsPosted as seen in the picture, so I want to add another job, I have dishwasher and waiter already. But I get an error with this query

     db.collection("companies").doc("Tiradito").field("jobsPosted").set(postJobObject).then(function() {
        console.log("Document successfully written!");
       });

That's my postJobbject

var postJobObject = {
      "position": this.state.selected,
      "timeSchedule": this.state.timeSchedule,
      "compensation" : this.state.compensation,
      "experience" : this.state.experience,
      "description" : this.state.description
    }

Here is a link to an image of my firestore

like image 317
bility Avatar asked Dec 27 '17 08:12

bility


2 Answers

pass the option to merge the new data with any existing document to avoid overwriting entire documents

var cityRef = db.collection('cities').doc('BJ');

var setWithMerge = cityRef.set({
    capital: true
}, { merge: true });

source: https://firebase.google.com/docs/firestore/manage-data/add-data

like image 134
astrojams1 Avatar answered Nov 13 '22 18:11

astrojams1


Try

    jobsPosted = {}

    var postJobObject = {
          "position": this.state.selected,
          "timeSchedule": this.state.timeSchedule,
          "compensation" : this.state.compensation,
          "experience" : this.state.experience,
          "description" : this.state.description
        }

jobsPosted['newJob'] = postJobObject;

Then use update

db.collection("companies").doc("Tiradito").update(jobsPosted).then(function() {
        console.log("Document successfully written!");
       });
like image 34
Hareesh Avatar answered Nov 13 '22 16:11

Hareesh