[我正试图通过半径为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能否请您帮助我解决此问题。
我找到了答案。而不是在上面的查询中使用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 => {...
。。此后它开始工作]