Firebase从数据库检索错误的数据信息

时间:2019-02-04 06:28:41

标签: javascript html firebase firebase-realtime-database

我认为我的数据库嵌套得很好。对于我使用当前用户uid保存的每个数据,然后将其推送到firebase中,它会创建一个唯一的pushid,因此,当我想要获取数据时,它总是仅获取一个不是来自唯一pushid的数据,而是用户uid是正确的。如何从正确的pushid获取正确的数据?

我要保存信息的数据库:https://imgur.com/a/tird0jD

我要检索要保存在数据库另一个节点中的信息的数据库:https://imgur.com/a/50f1M8W

正如您在数据库图片中注意到的那样,uid和pushid可以正确检索,但是里面的数据检索错误,当我尝试全部操作时,似乎正在返回所有相同的数据。

下面是我的JS代码:

function choose() {

  assign(document.getElementById("updateUserID").value, document.getElementById("updateID").value,
    document.getElementById("person").value);

}

var rootRef2 = firebase.database().ref('Admin/Person In Charge/Towing');
select = document.getElementById('person');
var opt1 = document.createElement('Option');
opt1.value = "-Select-";
opt1.innerHTML = "-Select-";
select.appendChild(opt1);

//Retrieve P-I-C data from Firebase
rootRef2.on("child_added", function (pericRecord) {
  var opt = document.createElement('Option');
  opt.value = pericRecord.val().Name;
  opt.innerHTML = pericRecord.val().Name;
  select.appendChild(opt);
});





function showModal(userId, pushId) {
  $('#modal').modal("show");
  document.getElementById("updateID").value = pushId;
  document.getElementById("updateUserID").value = userId;

}

function assign(userId, pushId, x) {

  var person = x;
  var rootRef3 = firebase.database().ref('Users/Towing Request/'  + userId)

  rootRef3.on('child_added', function (snapshot) {
    document.getElementById("updateID").value = pushId;
    document.getElementById("updateUserID").value = userId;

    firebase.database().ref('Admin/AssignCarTowing/' + person +"/"+userId +"/"+pushId)
      .update({
        CustUID: userId,
        CustName: snapshot.val().Name,
        CustCarModel: snapshot.val().CarModel,
        CustCarNumber: snapshot.val().CarNumber,
        CustContactNo: snapshot.val().ContactNo,
        CustLocation: snapshot.val().BreakdownLocation,
        CustBDTD: snapshot.val().TowingDateandTime
      })

  });


  firebase.database().ref('/Users/Towing Request/' + userId + "/" + pushId)
    .update({ PersonInCharge: person, Status: "Assigned" });
  window.alert("Updated to Assigned with Person In Charge");


  firebase.database().ref('/Users/Towing Request/' + userId + "/" + pushId).once('value').then(function (snapshot) {

    //document.getElementById('name').value = snapshot.val().Name;
    //document.getElementById('email').value = snapshot.val().Email;
    //document.getElementById('peric').value = snapshot.val().PersonInCharge;
    //document.getElementById('status').value = snapshot.val().Status;

    window.alert("Retrieved");
    location.reload();

  });
}

我的html代码,我选择将谁保存到其中:

<div class="modal fade" id="modal" role="dialog">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                                <h4 class="modal-title">Choose Person In Charge</h4>
                            </div>
                            <div class="modal-body">
                                <input type="text" id="updateID" />
                                <input type="text" id="updateUserID" />


                                <select id="person" onchange=choose()>

                                </select>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                            </div>
                        </div>
                    </div>
                </div>

我从获得的所有搜索中尝试了所有可能的答案,但还是没有运气。

1 个答案:

答案 0 :(得分:0)

我设法通过代码的反复试验来解决这个问题。以下是帮助我解决此问题的代码。

function assign(userId, pushId, person) {


  var rootRef3 = firebase.database().ref('Users/Towing Request/' + userId);

  rootRef3.on('value', function (snapshot) {
    console.log(snapshot.key)

    firebase.database().ref('Admin/AssignCarTowing/' + person + "/" + userId + "/" + pushId)
      .set({
        CustUID: userId,
        CustName: snapshot.child(pushId).val().Name,
        CustCarModel: snapshot.child(pushId).val().CarModel,
        CustCarNumber: snapshot.child(pushId).val().CarNumber,
        CustContactNo: snapshot.child(pushId).val().ContactNo,
        CustLocation: snapshot.child(pushId).val().BreakdownLocation,
        CustBDTD: snapshot.child(pushId).val().TowingDateandTime



      })


  });


  firebase.database().ref('/Users/Towing Request/' + userId + "/" + pushId)
    .update({ PersonInCharge: person, Status: "Assigned" });
  window.alert("Updated to Assigned with Person In Charge");


  //firebase.database().ref('/Users/Towing Request/' + userId + "/" + pushId).once('value').then(function (snapshot) {

  //document.getElementById('name').value = snapshot.val().Name;
  //document.getElementById('email').value = snapshot.val().Email;
  //document.getElementById('peric').value = snapshot.val().PersonInCharge;
  //document.getElementById('status').value = snapshot.val().Status;

  //window.alert("Retrieved");
  location.reload();

  //});
}

仅功能分配被更改。