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!
Aucun commentaire:
Enregistrer un commentaire