【发布时间】:2021-12-05 00:26:58
【问题描述】:
我有这段代码,我试图从时间不超过半小时前的集合中获取文档。它在不等式运算符中完全失败。
try {
const HALF_HOUR = 0.1 * 6000 * 6000 / 2;
const tsToMillis = firebase.firestore.Timestamp.now().toMillis();
const currenttime = new Date(tsToMillis - HALF_HOUR);
const myCurrentLoc = location[0]; // my device location
const latitude = myCurrentLoc.latitude;
const longitude = myCurrentLoc.longitude;
const geocollection = GeoFirestore.collection('chats');
const query = await geocollection.limit(1).near({ center: new firebase.firestore.GeoPoint(latitude, longitude), radius: 0.03 });
query.where('details.time', '>=', currenttime).get().then((querySnapshot) => {
if (!querySnapshot.empty) {
querySnapshot.docs.forEach(documentSnapshot => {
if (documentSnapshot.exists) {
this.fetchAllMessages(documentSnapshot.id);
}
});
}
}).catch(error => {
console.error("Error in fetch_messages", error);
});
} catch (e) {
console.error("Something happened: ", e); // ERROR IS TRIGGERED HERE
}
C:\MyProjects\BLEConnect\node_modules\react-native\Libraries\Core\ExceptionsManager.js:180 发生了一些事情:错误:firebase.firestore().collection().orderBy() 查询无效。初始 Query.orderBy() 参数:g.geohash 必须与 Query.where() fieldPath 参数相同:在调用不等式运算符时的 details.time
此错误仅在调用不等运算符而不是“==”等运算符时发生。为什么这样?我就是不明白。
P.S.:“时间”字段是在创建 geohash 时添加的,所以在同一个查询中:
const tsToMillis = firebase.firestore.Timestamp.now().toMillis();
const currenttime = new Date(tsToMillis);
const geocollection = GeoFirestore.collection('chats');
geocollection.add({
name: 'Geofirestore',
uid: device_id,
coordinates: new firebase.firestore.GeoPoint(latitude, longitude),
time: currenttime
})
【问题讨论】:
-
请复制并粘贴实际错误。我很难相信错误会说“发生了一些事情”。 (如果这是字面上的错误,firebase 人员需要做一些解释)
-
@Mike'Pomax'Kamermans 我做了,但我想我错过了那部分。
标签: javascript firebase react-native google-cloud-firestore geofirestore