简体   繁体   中英

How to delete a specific child from firebase realtime database using javascript?

i'm creating a game which stores it data in firebase database. Suppose When i touch a key it sets left:1 in database. How can i delete this left:1 when i remove the touch from that key.

The below code shows how i set left:1 in database.

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:

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:

document.getElementById('moveleft').addEventListener('touchend',()=>{
    touchRef.remove()
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM