为了账号安全,请及时绑定邮箱和手机立即绑定

等到在 Firebase 数据库(网络)中检测到新数据

等到在 Firebase 数据库(网络)中检测到新数据

慕无忌1623718 2022-05-26 14:15:46
在此示例中,我想在 firebase 数据库的父节点中创建一个名为“friend”的新节点时更改站点。我不知道如何让它等到它检测到新节点。    var friendUid;    firebase.database().ref('users/' + uid).child('friend').on('value', function(snapshot){       friendUid = snapshot.val();    })    while(!friendUid){ //I've also tried with "friendUid == null"     firebase.database().ref('users/' + uid).child('friend').on('value', function(snapshot){       friendUid = snapshot.val();     })    }    window.document.location = "whatever.html";
查看完整描述

1 回答

?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

您不需要循环,但您确实需要将更改位置的代码移动到侦听器中:


firebase.database().ref('users/' + uid).child('friend').on('value', function(snapshot){

  var friendUid = snapshot.val();

  if (friendUid == "what you're looking for") {

    window.document.location = "whatever.html";

  }

})

如果您只想检查节点是否存在,可以简化为:


firebase.database().ref('users/' + uid).child('friend').on('value', function(snapshot){

  if (snapshot.exists()) {

    window.document.location = "whatever.html";

  }

})


查看完整回答
反对 回复 2022-05-26
  • 1 回答
  • 0 关注
  • 123 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号