gpt4 book ai didi

javascript - Firebase - Geofire 和云功能。功能结束是否意味着不再有听众?

转载 作者:行者123 更新时间:2023-11-30 09:25:58 25 4
gpt4 key购买 nike

在我的云函数的 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 的运行和退出范例不匹配。

这些概念(在 geohashes 中存储 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();
});
});
})
})

关于javascript - Firebase - Geofire 和云功能。功能结束是否意味着不再有听众?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49100433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com