最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - can't get the key of a datasnapshot - Stack Overflow

programmeradmin1浏览0评论

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 became snapshot.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
Add a ment  | 

1 Answer 1

Reset to default 8

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.

发布评论

评论列表(0)

  1. 暂无评论