在我的云函数的index.js
文件中,我有以下函数体:
exports.onSuggestionCreated = functions.firestore.document('suggestions/{userId}').onCreate(event => {
return admin.firestore().doc(`places/settings/profile`).get().then(doc => {
[...]
const location = data.location
const ref = admin.database().ref(`suggestion_locations`)
const geoFire = new GeoFire(ref)
var geoQuery = geoFire.query({
center: [location.latitude, location.longitude],
radius: 1.0
})
geoQuery.on("key_entered", function(key, location, distance) {
return sendMessageTo(key, title, body)
})
})
})
这是在创建某些内容时触发的函数内部。
我要知道的是,即使云功能早已被终止,每次进入由GeoFire
的位置和半径分隔的区域时,都会调用“key_entered”吗?我有一些奇怪的日志表明了这一点。
鉴于GeoFire
的异步性质,在这种情况下我该怎么办?
GeoFire依赖于范围内的keeping active listeners on the geodata。这与Cloud Functions的run-and-exit范例不匹配。
概念(在地理位置存储lat + lon和运行范围查询)工作正常,但您可能必须修改库或注意其实现细节,以使其适用于您的情况。
最好的似乎是返回例如当前在给定区域内的所有位置。这可以通过听key_entered
事件(正如你已经做过)和ready
事件(fires after the initial key_entered
calls have been received)来完成。
exports.onSuggestionCreated = functions.firestore.document('suggestions/{userId}').onCreate(event => {
return admin.firestore().doc(`places/settings/profile`).get().then(doc => {
[...]
const location = data.location
return new Promise(function(resolve, reject) {
const ref = admin.database().ref(`suggestion_locations`)
const geoFire = new GeoFire(ref)
var geoQuery = geoFire.query({
center: [location.latitude, location.longitude],
radius: 1.0
})
geoQuery.on("key_entered", function(key, location, distance) {
sendMessageTo(key, title, body)
})
geoQuery.on("ready", function() {
resolve();
});
});
})
})