delete entire record which is associated with a certain child value

隐身守侯 提交于 2021-01-29 15:17:51

问题


i want to delete the entire record in the database using the child value entered this is the code i tried for it

<body>

   User Name: 
   <input type="text" name="user_name" id="user_name" placeholder="enter text"/><br><br>
   Enter Email:
  <input type="text" name="user_name" id="user_name2" placeholder="enter email" />

<br><br>


    <input type="button" value="Save" onclick="save_user();" />
    <input type="button" value="delete" onclick="delete_user();" />

<script>

  var tblUsers = document.getElementById('tbl_users_list');
  var databaseRef = firebase.database().ref('users/');
  var rowIndex = 1;
  var uid;
  var childKey;
  var childData;

  databaseRef.once('value', function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
    childSnapshot.key;
    childSnapshot.val();

   var row = tblUsers.insertRow(rowIndex);
   var cellId = row.insertCell(0);
   var cellName = row.insertCell(1);
   cellId.appendChild(document.createTextNode(childKey));
   cellName.appendChild(document.createTextNode(childData.user_name));
   cellName.appendChild(document.createTextNode(childData.user_name2));

   rowIndex = rowIndex + 1;


    });
  });


  function save_user(){
   var user_name = document.getElementById('user_name').value;
   var user_name2 = document.getElementById('user_name2').value;

   uid = firebase.database().ref().child('users').push().key;

   var data = {
    user_id: uid,
    user_name: user_name,
    password: user_name2
   }

   var updates = {};
   updates['/users/' + uid] = data;
   firebase.database().ref().update(updates);

   alert('The user is created successfully!');
   reload_page();

   document.writeln(uid);


  }

  function update_user(){
   var user_name = document.getElementById('user_name').value;
   var user_id = document.getElementById('user_id').value;

   var data = {
    user_id: user_id,
    user_name: user_name
   }

   var updates = {};
   updates['/users/' + user_id] = data;
   firebase.database().ref().update(updates);

   alert('The user is updated successfully!');

   reload_page();
  }

  function delete_user(){
   var user_id = document.getElementById('user_id').value;

   firebase.database().ref().child('/users/' + user_id).remove();
   alert('The user is deleted successfully!');
   reload_page();
  }

here the child value that to be refered and deleted is to be user_name2

all i want from this code is to delete the record of the child value associated with it

my database looks like

for example i want to delete the entire record which is associated with a child name password "ddd"


回答1:


Firebase can only write to a node if it knows the complete, exact path to that node. If you only know a property value of the node, you'll first need to execute a query to find the nodes with that value, and then write those nodes.

So in your case that'd be something like:

let password = "ddd"; // hard-coded value, but this could also be read from the HTML

let ref = firebase.database().ref("users");
let query = ref.orderByChild("password").equalTo(password);

query.once("value").then(function(results) {
  results.forEach(function(snapshot) {
    snapshot.ref.remove();
  })
})


来源:https://stackoverflow.com/questions/60252141/delete-entire-record-which-is-associated-with-a-certain-child-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!