在我的云函数中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)
})
})
})
这是在一个函数内部,每当创建某些东西时就会触发该函数。
我想知道的是,每次有东西进入由位置和半径界定的区域时,是否都会调用“key_entered”GeoFire
即使云功能早已终止?我收到一些奇怪的日志表明了这一点。
鉴于异步性质GeoFire
,在这种情况下我能做什么?
GeoFire 依赖于让地理数据保持活跃的听众 https://github.com/firebase/geofire-js/blob/master/src/geoQuery.js#L305-L307即在范围内。这与 Cloud Functions 的运行和退出范例不匹配。
这些概念(在 geohashes 中存储 lat+lon 并对其运行范围查询)工作得很好,但您可能必须修改库或注意其实现细节才能使其适合您的情况。
最好的方法似乎是返回例如当前位于给定区域内的所有位置。这可以通过聆听来完成key_entered
事件(就像你已经做的那样)and to the ready
事件,其中初始后触发key_entered已接到电话 https://github.com/firebase/geofire-js/blob/master/docs/reference.md#geoqueryoneventtype-callback.
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();
});
});
})
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)