简体   繁体   English

如何在Firebase上添加Child,然后获取并显示信息?

[英]How to Add Child on Firebase and then Get it and Display the information?

I'm trying to understand Firebase and I've been reading the useless Docs and looking on Here but none seem to help me understand the basics of adding and displaying from Firebase. 我正在尝试了解Firebase,我一直在阅读无用的文档并查看此处,但似乎没有一个可以帮助我理解从Firebase添加和显示的基础知识。 I'm trying to add a Child to Salesperson containing the name and number of cars sold so I can then retrieve them and display them on a table. 我正在尝试将一个Child添加到Salesperson,其中包含销售的汽车的名称和数量,以便我可以检索它们并将它们显示在桌子上。

var salespersonRef = database.ref('Salespersons');

  $("#add-user").on("click", function(event) {
    event.preventDefault();

    name = $("#name-input").val().trim();
    cars_sold = $("#cars_sold-input").val().trim();

    salespersonRef.child({
      name: name,
      cars_sold: cars_sold,
    });


 salespersonRef.on("value", function(snapshot) {

    // Log everything that's coming out of snapshot
    console.log(snapshot.val());
    console.log(snapshot.val().name);
    console.log(snapshot.val().cars_sold);
    // Change the HTML to reflect
    $("#name-display").text(snapshot.val().name);
    $("#cars_sold-display").text(snapshot.val().cars_sold);

So thats what I have at the moment for adding and displaying. 这就是我目前所拥有的添加和显示内容。 It does display the current addition but it overwrites anything I add and doesnt make another child 它确实显示当前的添加但它会覆盖我添加的任何内容并且不会生成另一个孩子

This code doesn't do anything: 此代码不执行任何操作:

salespersonRef.child({
  name: name,
  cars_sold: cars_sold,
});

If you're trying to add a new child under salespersonRef , it should be: 如果您尝试在salespersonRef下添加新子项,则应该是:

salespersonRef.push({
  name: name,
  cars_sold: cars_sold,
});

Every time you can push() it generates a new unique location under that reference. 每次你可以push()它会在该引用下生成一个新的唯一位置。

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

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