我想创建一个带有firebase的javascript的学生考勤跟踪器应用程序。Firebase查询里面的函数返回null
但我有一个问题,我试图查询一个函数内部并让函数返回这些数据。
但是,当我尝试,它什么都没有返回。 '未定义'
这里是我的代码
var root = 'https://path.firebaseio.com';
function initFireSubjects(){
var db = {};
db.connection = new Firebase(root + '/subjects');
db.addSubject = function (year, code, name) {
this.connection.child(year).push({
code: code,
name: name
});
};
db.getAll = function (year) {
var value;
this.connection.child(year).on('value', function (snapshot) {
value = snapshot.val();
});
return value;
};
}
,这里是我的代码来测试它
var db = initFireSubjects();
var test = db.getAll('2014');
console.log(test);
数据的结构看起来像这样
subject:{
'2014':{
randomKey1:{
code:'eng1',
name:'English 1'
},
randomKey2:{
code:'math1',
name:'Mathematics 1'
},
randomKey3:{
code:'sci1',
name:'Science 1'
}
},
'2015':{
randomKey4:{
code:'polsci1',
name:'Political Science 1'
},
randomKey5:{
code:'comprog',
name:'Computer Programming'
}
}
}
的情况下,是我可以拉数据,如果我这样做的话
db.getAll = function(year){
var value;
this.connection.child(year).on('value',function(snapshot){
value = snapshot.val();
console.log(value);
}
}
我不知道为什么它不能以面向对象的方式提取数据。
任何输入?
谢谢。
谢谢你回复良好的先生。这是很好的信息。 – 2015-02-07 14:14:00
不客气。如果答案有用,请按下向上箭头投票。如果它回答您的问题,请点击旁边的复选标记接受答案。 – 2015-02-07 14:43:18