3

I am trying to add the json data through the node to firestore database earlier it was working but now it is giving me error. I referred this question on SO to do the same QUESTION below is the json data and the code used to implement the upload through node.

JSON Data

[
{
    "itemID": "MOMOS_NV_101",
    "itemName": "Sangai Momos",
    "itemPriceHalf": 90,
    "itemPriceFull": 150,
    "hasImage": false,
    "itemCategory": "momos_nv",
    "itemType": "nv",
    "geopoint": {
        "lat": 1,
        "long": 1
    }
  },
  {
    "itemID": "MOMOS_NV_102",
    "itemName": "Kadi Veg Momos",
    "itemPriceHalf": 80,
    "itemPriceFull": 130,
    "hasImage": false,
    "itemCategory": "momos_nv",
    "itemType": "nv",
    "geopoint": {
        "lat": 1,
        "long": 1
    }
  }
]

javascript

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

To upload the code through VS Code terminal I first install firebase tools through this command npm install -g firebase-tools and then used the command node uploader.js to upload file to firebase database, I even have to configure the service_key.json file in firebase account.

Now when I execute the command I face issue at these to lines of the code var menu = require("./files/" + file); and files.forEach(function(file)

Error I get is here

SyntaxError: Invalid or unexpected token

please guide me the resolution for the same.

0 Answers0