简体   繁体   English

从Firebase Web检索数据

[英]Retrieving data from Firebase Web

Been trying multiple different ways to get this to work. 一直在尝试多种不同的方法来使其正常工作。 The aim is to have a welcome box that doesn't appear if the user has seen it before. 目的是要有一个欢迎框,如果用户以前见过它,它不会出现。 If the Active field in my database shows a 1 the welcome div should not be visible. 如果数据库中的活动字段显示为1,则欢迎div应该不可见。

document.onload = function() {
  var active
  var user = firebase.auth().currentUser;
  var uid;

  if (user != null) {
    uid = user.uid;
  }

  firebase.database().ref("Users/" + uid + '/Active').on("value", function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
      var active = childSnapshot.val();
      if (active = 1) {
        document.getElementById("welcome").style.display = "none";
        document.getElementById("box").style.display = "none";
        document.getElementById("title").style.display = "none";
        document.getElementById("text").style.display = "none";
      }
    });
  });
};

在此处输入图片说明

Thank you in advance for any help or advice. 预先感谢您的任何帮助或建议。

You need to read the property of the object - childSnapshot.val().active. 您需要读取对象的属性-childSnapshot.val()。active。

Respectfully, you're going about it backwards. 顺便说一句,您正在反向进行此操作。 If the user is not in the database, you want to show the welcome card. 如果用户不在数据库中,则要显示欢迎卡。 If the user is in the database, they've probably already interacted with the app and seen the welcome card. 如果用户在数据库中,则他们可能已经与该应用程序进行了交互并看到了欢迎卡。

The welcome card should be hidden by CSS display: none so it does not blink or show at all on page load. 欢迎卡应该由CSS display: none隐藏display: none因此不会在页面加载时闪烁或完全显示。

Use a boolean, native true or false (notice no quotes around the word) rather than a 1 or 0, in my opinion. 在我看来,请使用布尔值,本机的truefalse (注意,单词周围不带引号)而不是1或0。 Initialize var showWelcomeCard = false at the top of your script. 在脚本顶部初始化var showWelcomeCard = false

Once you determine that you should show the welcome card, show it; 一旦确定要出示欢迎卡,请出示; not the other way around. 并非相反。

Lastly don't assume they don't want to see the welcome/start card again. 最后,不要以为他们不想再看到欢迎/开始卡。 Offer them a choice. 给他们一个选择。 A checkbox saying "Don't show this again". 一个复选框,上面显示“不再显示”。 So, change the property from active to showWelcomeCard . 因此,将属性从active更改为showWelcomeCard

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM