I am trying to get the key of a snapshot with the
dataSnapshot.key()
method, but it doesn't seem to be working. Here is the related code:
index.html:
...
<select id="resList" size="20"></select>
...
index.js:
function addChild(name, id) {
var list = document.getElementById("resList");
var item = document.createElement("option");
item.text = "Resolution " + id + ": " + name;
list.add(item);
}
function changeChild(name, index) {
var list = document.getElementById("resList");
var item = document.createElement("option");
item.text = "Resolution " + (index+1) + ": " + name;
list.remove(index);
list.add(item, index);
}
function removeChild(index) {
var list = document.getElementById("resList");
list.remove(index);
}
function init() {
const resolutionRef = firebase.database().ref().child('resolutions');
resolutionRef.on('child_added', function(childSnapshot, prevChildKey) {
if (prevChildKey == null)
prevChildKey = "0";
addChild(childSnapshot.val(), parseInt(prevChildKey) + 1);
});
resolutionRef.on('child_changed', function(childSnapshot, prevChildKey) {
if (prevChildKey == null)
prevChildKey = "0";
changeChild(childSnapshot.val(), parseInt(prevChildKey));
});
resolutionRef.on('child_removed', function(oldChildSnapshot) {
removeChild(parseInt(oldChildSnapshot.key()));
});
}
window.onload = init;
I have a child of the 'root' reference called 'resolutions'. For each resolution a new child to the 'resolutions' reference is created. Each resolution has a name and an ID. I'm storing the ID of each resolution as the key and it's name as the value. This is convenient because this way I can determine the index of a resolution in 'resList' by simply subtracting one of it's key.
The above code works fine for adding and changing children, but for some reason when I remove a child nothing happens.
Thanks for any help in advance!
As @FrankVanPuffelen suggested, it seems 'key' is no longer a function, but rather a variable. So
var key = dataSnapshot.key;
does work, but
var key = dataSnapshot.key();
doesn't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With