gpt4 book ai didi

javascript - 检索现有数据后 Firebase 运行脚本

转载 作者:行者123 更新时间:2023-11-30 11:52:46 24 4
gpt4 key购买 nike

我在 js 中有一个函数需要在我从 Firebase 检索数据后刷新。

firebase.database().ref('/users/' + this.userId + '/info').on('child_added', function(snapshot){
this.myFunction();
}.bind(this));

问题是 Firebase 中的数据非常大,所以我不知道要多次运行该函数。这就是我运行它的方式:

  1. 从 Firebase 中检索所有现有数据。
  2. 运行函数(一次)。
  3. 每次从 Firebase 收到更多数据时,再次运行该函数。

这可能吗?

谢谢。

最佳答案

是的,这是可能的。首先,您需要将 ref 参数留空,以便从根节点(所有数据)获取所有信息。由于参数'value',它将在默认情况下首次运行时触发,并会在任何数据更改时再次触发。您可以代替您使用 child_added、child_changed、child_removed 或 child_moved 来减少从 firebase 接收数据的次数。示例:

firebase.database().ref().on('value', function(snapshot){
myFunction(snapshot.val());
};

附言不要忘记使用 snapshot.val() 从快照中提取数据。

关于javascript - 检索现有数据后 Firebase 运行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38969411/

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