如何在函数javascript中传递firebase值/ key作为参数

时间:2016-02-29 14:44:51

标签: javascript firebase

是否可以将数据所在的键作为参数传递给函数?

例如;

function myfirebasedata(myDivTag, value){
  var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
  ref.on('child_added', function(snapshot) {
    var data = snapshot.val();    
    myDivTag.innerHTML = data.value.age;
  });    
}

然后我可以这样称呼:

myfirebasedata(divTagAgeJames, James);
myfirebasedata(divTagAgeBob, Bob);
....
....

例如,当函数的第一个实例运行/调用myfirebasedata(divTagAgeJames, James);

它应该替换上面firebase代码的引用中的关键字;

var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
ref.on('child_added', function(snapshot) {
  var data = snapshot.val();    
  myDivTagJames.innerHTML = data.James.age;
});

更新

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以尝试其中之一:

1。snapshot.child()

ref.on('child_added', function(snapshot) {
    var age = snapshot.child(value).val().age;    
    myDivTag.innerHTML = age;
});

2。bracket notation

ref.on('child_added', function(snapshot) {
    var data = snapshot.val();    
    myDivTagJames.innerHTML = data[value].age;
});