【问题标题】:How to upload GeoPoint field type test data into Firestore database using Node.js如何使用 Node.js 将 GeoPoint 字段类型测试数据上传到 Firestore 数据库
【发布时间】:2020-06-09 03:24:13
【问题描述】:

我正在使用 Node.js 将测试数据上传到 Firestore 数据库。在每个文档的数据集中,我都有一个 GeoPoint 字段。我无法弄清楚如何上传数据类型:Firestore 字段格式的 GeoPoint,可以手动完成,您可以在字段类型 GeoPoint 下输入纬度和经度作为 2 个单独的条目。我的 Node.js 仅作为单个“字符串”上传

对我的代码的任何帮助将不胜感激..!

参考: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);
        });
    });
}

【问题讨论】:

  • 您应该能够在文档中设置一个点,如下所示:stackoverflow.com/a/62103613,然后可能使用new admin.firestore.GeoPoint(latvalue, lonvalue)

标签: node.js firebase google-cloud-firestore geopoints


【解决方案1】:

将成员评论添加为社区 Wiki 以获得更好的可见性。

Firestore GeoPoint 对象的创建记录在 here。此外,您可以在 API documentation for Firestore 中找到代码示例。

你也可以看看这个thread

【讨论】:

    猜你喜欢
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 2018-06-06
    • 2018-09-03
    • 2022-01-27
    相关资源
    最近更新 更多