我在我的应用程序中对作业和用户进行了地理定位。我在index.js中创建了2个全局索引,如下所示:
const geousers = new GeoFire(admin.database().ref().child('geolocation').child('users'));
const geojobs = new GeoFire(admin.database().ref().child('geolocation'.child('jobs')));
我在我的函数中执行geoQuery:
exports.nearbyjobs = functions.database.ref("/users/{userid}/account/l").onWrite((event) => {
const location = event.data;
if (location.child("lat").val() === null || location.child("lng").val() === null) return false;
const uid = event.params.userid;
let lat = Number(location.child("lat").val());
let lng = Number(location.child("lng").val());
const geoQuery = geojobs.query({
center: [lat, lng],
radius: 0.7
});
return admin.database().ref(`users/${uid}/nearbyjobs`).remove().then(data => {
geoQuery.on("key_entered", function(key, location, distance) {
return Promise.all([admin.database().ref(`jobs/${key}/category`).once('value'), admin.database().ref(`users/${uid}/account/categories`).once('value')]).then(r => {
const promises = [];
const cP = r[0];
const cO = r[1];
if (cO.val().includes(cP.val())){
return admin.database().ref(`users/${uid}/nearbyjobs/${key}`).set({
jobkey: key,
distance: distance,
l: location
});
}
});
});
});
});
在我的功能控制台中,我收到警告:
FIREBASE警告:使用未指定的索引。你的数据将是 在客户端上下载并过滤。考虑添加" .indexOn":" g" at / geojobs符合您的安全规则以获得更好的性能。
地理位置的数据库结构:
geolocation
|
-users
|
+<key>
|
-jobs
|
+<key>
答案 0 :(得分:0)
首先,您确定数据库的文档名称是作业吗?根据错误,我猜你已经创建了一个文件存储作为geojobs。 无论如何,假设您的文档存储名称是geojobs,这就是您解决上述错误的方法 您必须在geojobs下添加规则。转到Firebase控制台 - &gt;数据库 - &gt;规则。
你会看到类似下面的内容,
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
您必须通过添加.indexOn
将其更改为以下内容。
{
"rules": {
".read": "auth != null",
".write": "auth != null",
"geojobs": {
".indexOn": ["g"]
}
}
}