javascript - 如何将数组中的 JSON 数据保存到 Firestore

标签 javascript google-cloud-firestore

我正在尝试将 JSON 数据保存到 Firestore。 数据如下:

[{"back":"democracy","delayed_till":{"seconds":1574944200,"nanoseconds":0},"examples":[{"answer":"en Espana existe democracia desde 1975","example":"There is democracy in Spain since 1975"},{"answer":"Debemos luchar por nuestra democracia","example":"We must fight for our democracy"}],"front":"la democracia"},{"back":"habit, custom","delayed_till":{"seconds":1575148500,"nanoseconds":0},"front":"la costumbre"},{"back":"krokodil","delayed_till":{"seconds":1577831400,"nanoseconds":0},"front":"roos"},{"back":"krokodil","front":"blabla"},{"back":"bjkla","front":"blabla"}]

下面将其保存到 Firestore 不起作用,因为 JSON 数据存储在数组中

db.collection('cardsb').add(object)

使用以下命令仅保存 JSON 数据中的第一个对象。

db.collection('cardsb').add(object[0])

将保存的部分如下:

{"back":"democracy","delayed_till":{"seconds":1574944200,"nanoseconds":0},"examples":[{"answer":"en Espana existe democracia desde 1975","example":"There is democracy in Spain since 1975"},{"answer":"Debemos luchar por nuestra democracia","example":"We must fight for our democracy"}],"front":"la democracia"}

如何将所有数据保存在一个文档中?

编辑

这就是 JSON 数据的创建方式:

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(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(object[0])
      }

这就是它应该在 FB 中的存储方式:

enter image description here

这就是实际存储的内容

enter image description here

最佳答案

只需在发送之前对 JSON 进行字符串化,它将作为字符串存储在 firestore 中,当您需要检索它时,只需解析它即可。

示例。

const jsonArray = JSON.stringify({
  foo: bar,
  sam: mas,
  hello: "world",
  createdAt: Date.now()
})

await firebase.firestore()
  .collection('COLLECTION_NAME')
  .doc("DOC_ID")
  .update({
    arrayField: firebase.firestore.FieldValue.arrayUnion(jsonArray)
  })
  .then(async (resp) => { })
  .catch((error) => {
    console.log(error.code)
  });

这样您就可以将 json 数据保存为 firestore 中数组字段内的字符串

当您需要取回它时,您所要做的就是使用 JSON.parse

JSON.parse(fieldName)

您的数据将像这张图片一样存储

firestore image

关于javascript - 如何将数组中的 JSON 数据保存到 Firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59239278/

相关文章:

javascript - react : is componentDidUpdate same for 2 different instances of a component?

javascript - 点击后计算字符串中每个元音的数量

javascript - 如何解决 "The following modules couldn'不热更新: (They would need a full reload!)”

firebase - 您如何调试 Firestore 安全规则?

firebase - Firestore 在失去并重新获得互联网连接后停止更新

firebase - 如何限制 Firebase Firestore 集合中的文档

javascript - jQuery 1.5 仅在 ajax 方法中发送 GET 请求

javascript - 基于开关的 url 重定向

javascript - 无效查询。您不能使用多个 'in' 过滤器

Javascript firebase firestore 函数未执行