0

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"  
  } ] 

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

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

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

https://drive.google.com/file/d/1n_O_iKJWM5tR3HK07Glq6d65XAezEKLf/view

  • hope you are looking for this - https://stackoverflow.com/questions/46603691/how-to-save-geopoint-in-firebase-cloud-firestore – Muthu Thavamani Feb 03 '21 at 02:55
  • @MuthuThavamani sorry it did not resolve the purpose, it is saving the geopoint coordinates in either map or array datatype and not in the required datatype of `GeoPoint` –  Feb 03 '21 at 04:14
  • In Firestore, `GeoPoint` is an object and you could access like map. – Muthu Thavamani Feb 03 '21 at 04:16
  • @MuthuThavamani I am using this method to upload bulk data in firestore database https://drive.google.com/file/d/1n_O_iKJWM5tR3HK07Glq6d65XAezEKLf/view –  Feb 03 '21 at 04:21
  • 1
    @MuthuThavamani video link is here for the same https://www.youtube.com/watch?v=Qg2_VFFcAI8 –  Feb 03 '21 at 04:21
  • now, I got it! could you please update the file parsing and firstore write code? it will add more clarity for other viewers on what you tried so far.. – Muthu Thavamani Feb 03 '21 at 04:26
  • @MuthuThavamani added the link with main part of the code in the question above –  Feb 03 '21 at 04:33

1 Answers1

1

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

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

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

Muthu Thavamani
  • 1,325
  • 9
  • 19
  • I tried it but it is giving me error `TypeError: Cannot read property 'lat' of undefined` at `obj.geopoint.lat` –  Feb 03 '21 at 05:18
  • it means, you don't have a map field `geopoint` in your json. like one in my answer, "geopoint": { "lat": 1, "long": 1 } – Muthu Thavamani Feb 03 '21 at 05:20
  • ok let me see into that, I think I need to add it to every file and every document –  Feb 03 '21 at 05:42
  • Yes, it worked after the correction thanks –  Feb 04 '21 at 11:10