javascript - 等到在 Firebase 数据库(网络)中检测到新数据
问题描述
在此示例中,我想在 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";
解决方案
您不需要循环,但您确实需要将更改位置的代码移动到侦听器中:
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";
}
})
推荐阅读
- google-apps-script - 检索最新版本的 Google Doc 中的评论
- python - 如何从 CSV 文件中获取我的值以使用 python 加载到我的 SQL 数据库中?
- twitter-bootstrap - 将引导程序添加到 webpack
- python - Python:使用负载平衡运行多个 exe
- java - Spring Jpa Data Repository 使用 LinkedEntity 保存(更新)多对多关系
- java - 从 AsyncTask 获取 Int
- javascript - 在 vueJs 中修改使用 Apollo GraphQL 获取的属性
- graph - Web 负载测试 - 波浪形响应时间图表示什么?
- ruby - 如何通过 Ruby Selenium Binding(或 WATIR)创建 chrome 配置文件
- python - 参数解析期间的Python断言错误