Firebase Admin SDK事件监听器未触发。

huangapple go评论80阅读模式
英文:

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(&quot; Error: failed to create Listener %v\n&quot;, 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(&quot;%v\n&quot;, err)
			continue
		}
		fmt.Printf(&quot;Listener | Ref Path: %s | event.Path %s | event.Snapshot() = %v\n&quot;, ref.Path, event.Path, event.Snapshot())
		fmt.Printf(&quot;\n&quot;)
	}
}

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({&quot;value&quot;: value});

What fixed the problem was to use "set" instead of "update" the following way:

await ref.child(&quot;value&quot;).set(value);

This way my Go-Listener method then was finally triggered and everything was working as expected.

huangapple
  • 本文由 发表于 2021年9月6日 22:41:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/69076357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定