admin管理员组文章数量:1391947
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!
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!
Share Improve this question edited Aug 22, 2016 at 4:24 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges asked Aug 21, 2016 at 16:35 zomnombomzomnombom 9942 gold badges9 silver badges15 bronze badges 2-
3
What version of the Firebase JavaScript SDK are you using? In 2.x
snapshot.key()
was a function. In 3.x it changed to a read-only property, so would the equivalent becamesnapshot.key
. – Frank van Puffelen Commented Aug 21, 2016 at 21:04 - @FrankVanPuffelen It works! Thanks! (I am using firebase 3.3.0 by the way) – zomnombom Commented Aug 22, 2016 at 4:24
1 Answer
Reset to default 8As @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.
本文标签: javascriptcan39t get the key of a datasnapshotStack Overflow
版权声明:本文标题:javascript - can't get the key of a datasnapshot - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744711389a2621149.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论