首页 > 解决方案 > 我无法在 express 中使用 geofirestore 从我的 firestore db 获取数据

问题描述

我在 express 中创建了一个查询来提取我的数据,这是我的端点 express 函数:

app.get('/locatii', (req, res) => {
    const geofirestore = new GeoFirestore(db);
    const geocollection = geofirestore.collection('locatii2');
    const query = geocollection.near({
        center: new firebase.firestore.GeoPoint(45.831922, 27.431149),
        radius: 3000
    });
    query.get().then(snap => {
        console.log(snap.docs())
        let places = [];
        snap.forEach(doc => {
            places.push(doc.data());
            console.log(doc.data())
        });
        res.json(places);
    })
        .catch(err => res.send(err));
});

下面是我的痛点结构:数据库结构 当我访问我的端点时,它不会显示在控制台数据或邮递员中。我不知道我错过了什么。谢谢!

标签: javascriptexpressgoogle-cloud-firestoregeofirestore

解决方案


您的数据结构中似乎没有 geohash。为了能够按位置查询这些文档,需要在每个文档中都有一个 geohash。

从 GeoFirestoreJS 附带的示例应用程序中,您似乎可以通过以下方式获取 geohash

location.lat = ...
location.lng = ...
const hash = geokit.Geokit.hash(location);

然后将其设置到您的 Firestore 文档中,如下所示:

documentRef.update(hash);

推荐阅读