英文:
Firebase Admin SDK Event Listener not triggered
问题
我正在使用Go Firebase Admin SDK并监听实时数据库中的更改。
问题是,只有当我从Firebase控制台手动更新数据时,监听器才会触发。如果我从另一个应用程序(在这种情况下是Flutter)更改数据,即使在Firebase控制台中可以看到更改(因此数据肯定已更改),监听器也不会触发。
我甚至尝试通过Firebase数据库REST API进行更新,结果相同:数据更改在控制台中可见,但仍不会触发监听器。
以下是我在Go中监听更改的方式:
func listenToFirebase(ref *db.Ref, ctx context.Context) {
iter, err := ref.Listen(ctx)
if err != nil {
fmt.Printf("错误:无法创建监听器 %v\n", err)
return
}
defer iter.Stop()
for {
if iter.Done() {
break
}
event, err := iter.Next()
if err != nil {
// 根据具体用例处理错误
// 我们可以继续监听
log.Printf("%v\n", err)
continue
}
fmt.Printf("监听器 | 引用路径:%s | 事件路径:%s | 事件快照:%v\n", ref.Path, event.Path, event.Snapshot())
fmt.Printf("\n")
}
}
监听器通过从控制台更新数据来触发,这表明监听器正常工作。
附注:
Listen方法尚未集成到Go Firebase Admin SDK中,来源于https://github.com/firebase/firebase-admin-go/issues/229
英文:
I am using the Go Firebase Admin SDK and listening to changes in the Realtime Database.
The problem is that the listener is ONLY triggered if I manually update the data from the Firebase Console, if I change data from another app (in this case Flutter), the listener is NOT triggered even though the changes can be seen in the Firebase Console (so the data definitely changed).
I even tried performing an update via Firebase Database REST API <https://firebase.google.com/docs/reference/rest/database#section-streaming>
Which had the same result: Data changes are viewable in the Console, but still don't trigger the listener.
Here the way I'm listening to changes in Go:
func listenToFirebase(ref *db.Ref, ctx context.Context) {
iter, err := ref.Listen(ctx)
if err != nil {
fmt.Printf(" Error: failed to create Listener %v\n", err)
return
}
defer iter.Stop()
for {
if iter.Done() {
break
}
event, err := iter.Next()
if err != nil {
// Handle error here based on specific usecase
// We can continue Listening
log.Printf("%v\n", err)
continue
}
fmt.Printf("Listener | Ref Path: %s | event.Path %s | event.Snapshot() = %v\n", ref.Path, event.Path, event.Snapshot())
fmt.Printf("\n")
}
}
The fact that the listener is triggered by updating data from the Console, indicates that the listener is working properly.
P.S.:
The Listen-Method has not yet been integrated to the Go Firebase Admin SDK and is from <https://github.com/firebase/firebase-admin-go/issues/229>
答案1
得分: 0
原来问题并不在于Go代码。
问题出在我从Flutter更新实时数据库的方式上。
这是我用来执行更新的代码:
await ref.update({"value": value});
解决问题的方法是改用以下方式使用“set”而不是“update”:
await ref.child("value").set(value);
这样我的Go监听方法最终被触发,一切都按预期工作了。
英文:
Turned out there was no problem regarding the Go code.
The problem was the way I updated the Realtime Database from Flutter.
This is what I used to perform updates:
await ref.update({"value": value});
What fixed the problem was to use "set" instead of "update" the following way:
await ref.child("value").set(value);
This way my Go-Listener method then was finally triggered and everything was working as expected.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论