如何将具有嵌套数组的对象保存到Firestore

时间:2019-12-09 21:04:31

标签: javascript vue.js google-cloud-firestore

我有以下对象并将其保存到Firestore。

{
  "group": [{
      "back": "value",
      "tStamp": {
        "seconds": 1574944200,
        "nanoseconds": 0
      },
      "examples": [{
        "answer": "some text",
        "example": "some more text"
      }, {
        "answer": "some text",
        "example": "some more text"
      }],
      "front": "value"
    },
    {
      "back": "value",
      "tStamp": {
        "seconds": 1575148500,
        "nanoseconds": 0
      },
      "front": "value"
    },
    {
      "back": "value",
      "tStamp": {
        "seconds": 1577831400,
        "nanoseconds": 0
      },
      "front": "value"
    },
  ]
}

尝试用db.collection('cardsb').add(object)保存它会导致错误:

  

vue.runtime.esm.js?2b0e:619 [Vue警告]:v-on处理程序中的错误:“ FirebaseError:[code = invalid-argument]:函数DocumentReference.set()调用了无效数据。不支持的字段值:未定义”

我正在寻找一种将如上所述的数据(具有嵌套数组的对象)存储到firestore中的方法。我可以将其手动存储在Firestore中。见下文:

enter image description here

这是对象的生成方式:

cons () {
  const nbrGroups = this.cards.length
  for (let i = 0; i < nbrGroups; i++) {
    var object = []
    const nbrItems = this.cards[i].group.length
    for (let item = 0; item < nbrItems; item++) {
      var inputs = document.querySelectorAll('#' + this.cards[i].group_name.replace(/\s/g, '') + item + 'A #inFront, #' + this.cards[i].group_name.replace(/\s/g, '') + item + 'A #inBack, #' + this.cards[i].group_name.replace(/\s/g, '') + item + 'A #inAnswer, #' + this.cards[i].group_name.replace(/\s/g, '') + item + 'A #inExample')
      const examples = []
      if (inputs.length !== 2) {
        for (let i = 2; i < inputs.length; i++) {
          examples.push({ answer: inputs[i + 1].value, example: inputs[i].value })
          i++
        }
        object.push({
          back: inputs[1].value,
          delayed_till: this.cards[i].group[item].delayed_till,
          examples,
          front: inputs[0].value
        })
      } else {
        object.push({
          back: inputs[1].value,
          delayed_till: this.cards[i].group[item].delayed_till,
          front: inputs[0].value
        })
      }
    }
    console.log(JSON.stringify({ group: object }))
    console.log(JSON.stringify(object) === JSON.stringify(this.cards[i].group))
    console.log(JSON.stringify(this.cards[i].group))
    console.log(JSON.stringify(this.cards[i].id))

    db.collection('cardsb').add({ group: object })
  }
}

0 个答案:

没有答案