【问题标题】:Firebase retrieve the wrong data information from the databaseFirebase 从数据库中检索到错误的数据信息
【发布时间】:2019-02-04 06:28:41
【问题描述】:

我认为我的数据库嵌套得很好。对于我使用当前用户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>

我从所有搜索中尝试了所有可能的答案,但仍然没有运气。

【问题讨论】:

    标签: javascript html firebase firebase-realtime-database


    【解决方案1】:

    我自己通过反复试验代码来解决这个问题。下面是帮助我解决这个问题的代码。

    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");
    
      location.reload();
    }
    

    仅更改了函数分配。

    【讨论】:

      猜你喜欢
      • 2017-06-03
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多