【问题标题】:How to add array map to firestore database?如何将数组映射添加到 Firestore 数据库?
【发布时间】:2021-02-07 17:10:26
【问题描述】:

我可以通过节点将 json 数据添加到 firestore 数据库,如下 json 数据

[
  {
    "itemID": "NOODLES_V_101",
    "itemName": "Hakka Noodles",
    "itemPriceHalf": 80,
    "itemPriceFull": 130,
    "hasImage": false,
    "itemCategory": "noodles_v",
    "itemType": "v" 
  },
  {
    "itemID": "NOODLES_V_102",
    "itemName": "Shezwan Noodles",
    "itemPriceHalf": 80,
    "itemPriceFull": 130,
    "hasImage": false,
    "itemCategory": "noodles_v",
    "itemType": "v"
  },
]

我想通过 JSON 添加 firestore 数组映射,我应该怎么做? 我想要的数组映射类型如下面的截图所示

如果需要更多信息,请告诉我

我用来将 json 数据添加到 firestore 的代码如下 files.forEach(函数(文件){ var lastDotIndex = file.lastIndexOf(".");

var menu = require("./files/" + file);

menu.forEach(function(obj) {
  firestore
    .collection(file.substring(0, lastDotIndex))
    .doc(obj.itemID)
    .set(obj)
    .then(function(docRef) {
      console.log("Document written");
    })
    .catch(function(error) {
      console.error("Error adding document: ", error);
    });
});

}); 我有 json 文件,这些文件正在通过上述代码添加为 firestore 数据

【问题讨论】:

    标签: javascript node.js json firebase google-cloud-firestore


    【解决方案1】:

    您所要求的只是一个对象数组,类似于您在上面已经显示的内容。

    firebase
        .collection("your-collection")
        .doc("your-document")
        .set({
            ids: [
                { id: "1" },
                { id: "2" }
            ]
        })
    

    【讨论】:

    • 道格,我按照你说的尝试了,但没有成功,请检查上面的问题,我已经编辑了更多代码信息
    • 您可以尝试使用.set() 代替.add() 吗? .add() 将创建一个新文档,并自动生成一个 ID。
    猜你喜欢
    • 1970-01-01
    • 2017-05-24
    • 2022-01-05
    • 2020-11-02
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    相关资源
    最近更新 更多