删除与某个子值相关联的整个记录 [英] delete entire record which is associated with a certain child value

查看:31
本文介绍了删除与某个子值相关联的整个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用输入的子值删除数据库中的整个记录这是我尝试过的代码

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();
  }

此处要引用和删除的子值将为user_name2

我想从此代码中删除与之关联的子值的记录

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

我的数据库看起来像

例如,我要删除与孩子名字密码"ddd"相关的整个记录​​

推荐答案

Firebase仅在知道该节点的完整确切路径的情况下才能写入该节点.如果仅知道节点的属性值,则首先需要执行查询以查找具有该值的节点,然后编写这些节点.

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();
  })
})

这篇关于删除与某个子值相关联的整个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆