Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

javascript - Remove a child from firebase without knowing the key

database

I need to remove the unknown id child, but I know only the value. I've tried reading all values and inserting back all except that one. Is there any easy way to do this.

sample data: { unknownKey : knownValue , ...}

question from:https://stackoverflow.com/questions/65559755/remove-a-child-from-firebase-without-knowing-the-key

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can take in the wishlist object and loop through each key, value pair and find the value that matches, using the key. Then you can just remove it using the key you found.

//---use firebase code to get the wishlistObj---, for now I'll use another value
var wishlistObj = {"key1": "value1", "key2": "value2", "key3": "value3"};
var keysArray = Object.keys(wishlistObj);

for (var i = 0; i < keysArray.length; i++) {
    var key = keysArray[i];
    var value = wishlistObj[key];
    if (value === "value3") {
        //You know that the "key" variable is the value of the key of that value you know
        //Now you can use that key to take the value out of the firebase database
    
        //Try
        objectReference.child(key).removeValue() //Where the objectReference variable is the reference to the wishlist object in your database 
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...