在Firestore中添加一个字段?

时间:2017-12-27 08:00:28

标签: firebase google-cloud-firestore

我有一个名为jobsPosted的字段,如图所示,所以我想添加另一份工作,我已经有洗碗机和服务员了。但是这个查询出错了

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

这是我的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

2 个答案:

答案 0 :(得分:5)

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

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

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

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

答案 1 :(得分:1)

尝试

    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;

然后使用update

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