首页 > 解决方案 > 使用节点 js 将 JSON 写入云 Firestore

问题描述

我一直在使用如下所示的嵌套 JSON 文件。我想将此 JSON 写入使用 node.js 的 Firestore 文档。

{
"title":"Sample tittle",
"icon":{
    "type":"url/base64",
    "url":"http://www.sample.com/icon.png"
},
"steps":{
    "step1":{
        "type":"play",
        "url":"http://www.sample.com/"},
    "step2":{
        "type":"ask",
        "url":"http://www.sample.com/ab14",
        "Opts":["yes", "no"],
        "next":[
            {
                "id":"step1",
                "answer":"yes"
            },
            {
                "id":"step3",
                "answer":"no"
            }
        ]
    },
    "step3":{
       "type":"play",
        "url":"http://www.sample.com/ase"}
}

}

标签: node.jsjsongoogle-cloud-firestore

解决方案


它不应该有任何问题。你可以在 GitHub 上找到很好的参考资料

https://github.com/googleapis/nodejs-firestore/blob/master/README.md

我已经使用它并且能够从您的 JSON 中创建文档,如下所示:

const {Firestore} = require('@google-cloud/firestore');

// Create a new client
const firestore = new Firestore();

async function quickstart() {
  // Obtain a document reference.
  const document = firestore.doc('test_collection/test_document');


// Enter new data into the document.
  await document.set({
     <your JSON here>
  });
}
quickstart();

推荐阅读