Firestore文档中Geopoint的json格式 [英] json format for geopoint in firestore document

查看:65
本文介绍了Firestore文档中Geopoint的json格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够通过node成功将以下json数据保存到firestore数据库中.我想将"GeoPoint"以json格式保存到firestore数据库中,但无法弄清楚应如何在下面的json文件中写入内容.

I am able to save the below json data to the firestore database successfully through node. I want to save the 'GeoPoint' to the firestore database in the json format, which I am not able to figure out how should I write in the json file below.

 [ {     "itemID": "MOMOS_V_101",   
         "itemName": "Sangai Momos",   
         "itemPriceHalf": 70,   
         "itemPriceFull": 130,    
         "hasImage": false,     
         "itemCategory": "momos_v",   
         "itemType": "v"  
  } ] 

请提供以下格式:GeoPoint在json文件中应如何存储在Firestore数据库中.

Please provide the format how the GeoPoint should be there in the json file to be stored in the firestore database.

将批量JSON文件上传到Firestore数据库的代码

Code To Upload bulk JSON Files to firestore database

var admin = require("firebase-admin");

var serviceAccount = require("./service_key.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "YOUR_PROJECT_LINK"
});

const firestore = admin.firestore();
const path = require("path");
const fs = require("fs");
const directoryPath = path.join(__dirname, "files");

fs.readdir(directoryPath, function(err, files) {
  if (err) {
    return console.log("Unable to scan directory: " + err);
  }

  files.forEach(function(file) {
    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文件的完整代码

The complete code to write the json files is available at this link here

https://drive.google.com/file/d/1n_O_iKJWM5tR3HK07Glq6d65XAez

推荐答案

让我们考虑您的 Json 具有如下所示的geopoint字段,

let's consider your Json has geopoint field like below,

{     
     "itemID": "MOMOS_V_101",   
     "itemName": "Sangai Momos",   
     "itemPriceHalf": 70,   
     "itemPriceFull": 130,    
     "hasImage": false,     
     "itemCategory": "momos_v",   
     "itemType": "v",
     "geopoint": {
         "lat": 1,
         "long": 1
     }
}

要将其解析为Firestore Geopoint,您必须像下面一样更改迭代器

to parse this as Firestore Geopoint, you have to change your iterator like below

menu.forEach(function(obj) {
      obj.geopoint = new admin.firestore.GeoPoint(obj.geopoint.lat, obj.geopoint.long);

      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);
        });
    });

上面的解析器在写入之前将 geopoint 映射字段修改为Firestore Geopoint .

The above parser modifies the geopoint map field into Firestore Geopoint before write.

这篇关于Firestore文档中Geopoint的json格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆