【问题标题】:how to include both 'near' and 'where' methods in geo-queries using geofirestore library如何使用 geofirestore 库在地理查询中同时包含“near”和“where”方法
【发布时间】:2020-05-27 17:15:07
【问题描述】:

我正在尝试接近半径为 25 公里的“在线”用户,我正在使用以下代码使用 geofirestore-js 插件 (https://github.com/MichaelSolati/geofirestore-js) 执行地理查询

const geofirestore = new GeoFirestore(Firebase.firestore());
const geocollection = geofirestore.collection('location');
const query = geocollection.near({ center: coordinates, radius: 25 }).where('status' , '==' , 'online');
        query.onSnapshot(querysnapshot => {
            let nearByContact = []
            querysnapshot.docs.forEach((change) => {
                if (change.id !== store.user.uid) {
                    nearByContact.push({
                        coordinate: {
                            latitude: change.data().coordinates._latitude,
                            longitude: change.data().coordinates._longitude,
                        },
                        title: change.data().name,
                        description: change.distance.toFixed(2) + " km away",
                        image: change.data().avatarUrl,
                        profileId: change.id
                    })
                }
            })

同时使用 'near' 和 'where' 方法时返回 null。但是该查询适用于像这样的单个方法。

const query = geocollection.near({ center: coordinates, radius: 25 })
        query.onSnapshot(querysnapshot => {.....

我想通过这两种方法查询firestore db 你能帮我解决这个问题吗?

【问题讨论】:

    标签: firebase react-native google-cloud-firestore react-native-android geofirestore


    【解决方案1】:

    我找到了答案。而不是在上述查询中使用 query.onSnapshot 我将其更改为 query.get().then(querysnapshot => ...

    然后调试控制台上突然出现错误,说它需要 Firestore 索引

    Error: [firestore/failed-precondition] The query requires an index. You can create it here: ......
    

    然后我在 Firestore 中创建了索引并恢复了我对查询所做的更改(将 query.get().then(querysnapshot => {... 更改为 query.onSnapshot(querysnapshot => {...).. 之后它开始工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 1970-01-01
      • 2020-05-09
      相关资源
      最近更新 更多