向Firestore添加字段?

bil*_*ity 5 firebase google-cloud-firestore

如图中所示,我有一个名为JobsPosted的字段,所以我想添加另一个作业,我已经有洗碗机和侍者。但是我收到此查询错误

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

那是我的帖子

var postJobObject = {
      "position": this.state.selected,
      "timeSchedule": this.state.timeSchedule,
      "compensation" : this.state.compensation,
      "experience" : this.state.experience,
      "description" : this.state.description
    }
Run Code Online (Sandbox Code Playgroud)

这是我的消防站图像的链接

ast*_*ms1 7

传递选项以将新数据与任何现有文档合并,以避免覆盖整个文档

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

var setWithMerge = cityRef.set({
    capital: true
}, { merge: true });
Run Code Online (Sandbox Code Playgroud)

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


Har*_*esh 2

尝试

    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;
Run Code Online (Sandbox Code Playgroud)

然后使用update

db.collection("companies").doc("Tiradito").update(jobsPosted).then(function() {
        console.log("Document successfully written!");
       });
Run Code Online (Sandbox Code Playgroud)