[英]How to delete a specific child from firebase realtime database using javascript?
i'm creating a game which stores it data in firebase database. 我正在创建一个将数据存储在Firebase数据库中的游戏。 Suppose When i touch a key it sets left:1 in database.
假设当我触摸一个键时,它将在数据库中设置left:1。 How can i delete this left:1 when i remove the touch from that key.
我如何删除左键:1,当我从该键上删除触摸时。
The below code shows how i set left:1 in database. 下面的代码显示了我如何在数据库中设置left:1。
document.getElementById('moveleft').addEventListener('touchstart',()=>{
var left1 = left + 1;
console.log(left1);
var totalleft = firebase.database().ref('total left');
totalleft.push({
'left':left1
});
});
How can i delete this child(left) from the database when i remove the touch? 删除触摸后如何从数据库中删除此子级(左侧)?
To delete a node from the database you'll need to have a reference to that node. 要从数据库中删除节点,您需要引用该节点。 The easiest way to do this, is to keep the reference in a variable in the
touchstart
handler: 最简单的方法是将引用保留在
touchstart
处理程序中的变量中:
var touchRef;
document.getElementById('moveleft').addEventListener('touchstart',()=>{
var left1 = left + 1;
console.log(left1);
var totalleft = firebase.database().ref('total left');
touchRef = totalleft.push({
'left':left1
});
});
And then in touchend
handler you can use the reference to remove the nde: 然后在
touchend
处理程序中,您可以使用引用删除nde:
document.getElementById('moveleft').addEventListener('touchend',()=>{
touchRef.remove()
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.