如何将2个单独的变量上传到Firebase数据库中?

时间:2019-06-30 20:31:33

标签: javascript jquery firebase firebase-realtime-database

我正在尝试将2个变量上传到我的Firebase数据库中。第一个变量是唯一一个已上传的变量。变量newRoute正在上传,但新用户不是。我已经设置了整个网络应用的Fire配置,所以这不是问题。

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

  var pickUp = $("#pickUp-input").val().trim();
  var destination = $("#destination-input").val().trim();
  var time = moment($("#time-input").val().trim(), "hh:mm").format("hh:mm");  

  var newRoute = {
    pickup: pickUp,
    destination: destination,
    time: time
  };

  database.ref().push(newRoute);

  alert("Route successfully added");

  $("#pickUp-input").val("");
  $("#destination-input").val("");
  $("#time-input").val("");
});

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


  var newName = $("#name-input").val().trim();
  var passengers = $("#passengers-input").val().trim();

  var newUser = {
    name: newName,
    passengers: passengers
  };

  database.ref().push(newUser);


  alert("New Passengers successfully added");

  $("#name-input").val("");
  $("#passengers-input").val("");
});

database.ref().on("child_added", function (childSnapshot) {

  var pickUp = childSnapshot.val().pickup;
  var destination = childSnapshot.val().destination;
  var time = childSnapshot.val().time;

  var newName = childSnapshot.val().name;
  var passengers = childSnapshot.val().passengers;

1 个答案:

答案 0 :(得分:0)

您正在尝试通过单个引用child_added上的database.ref().on("child_added"....获取数据,似乎您也试图将这两个数据都推送到同一位置...

我建议您在refroutes之间分别设置一个:

passengers

Firebase Docs

中查看更多详细信息