Murray Henshall
Murray Henshall

Reputation: 1

How to upload GeoPoint field type test data into Firestore database using Node.js

I am using Node.js to upload test data into Firestore database. In my data set in each document I have a field that is a GeoPoint. I am unable to work out how to upload the data type: GeoPoint in the Firestore field format that can be done manually where you can enter both Latitude and Longitude as 2 seperate entries under the field type GeoPoint . my Node.js only uploads as a single "String"

Any help with my code would be appreciated.. !

Refer: https://firebase.google.com/docs/reference/node/firebase.firestore.GeoPoint

const admin = require('./node_modules/firebase-admin');
const serviceAccount = require("./**ServiceAccount**.json");

const data = require("./csvjson.json");
const collectionKey = "towns"; //name of the collection

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "**DatebaseURL**.com"
});

const firestore = admin.firestore();
const settings = {timestampsInSnapshots: true};
firestore.settings(settings);

if (data && (typeof data === "object")) {
    Object.keys(data).forEach(docKey => {
    firestore
        .collection(collectionKey)
        .doc()
        .set(data[docKey])
        .then((res) => {
            console.log("Document " + docKey + " successfully written!");
        })
        .catch((error) => {
            console.error("Error writing document: ", error);
        });
    });
}

Upvotes: 0

Views: 1353

Answers (1)

Happy-Monad
Happy-Monad

Reputation: 2002

Adding member comment as Community Wiki for better visibility.

The creation of Firestore GeoPoint objects is documented here. Moreover you could find code samples in the API documentation for Firestore.

You might also take a look at this other thread.

Upvotes: 0

Related Questions