【发布时间】:2015-04-07 11:52:18
【问题描述】:
我正在尝试使用带有 firebase 的 javascript 创建一个学生考勤跟踪器应用程序。
但是我有一个问题,我试图在函数内部进行查询并让函数返回这些数据。
但是当我尝试时,它什么也没有返回。 '未定义'
这是我的代码
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);
}
}
我不知道为什么它不能以面向对象的方式提取数据。
有什么意见吗?
谢谢。
【问题讨论】:
标签: javascript oop firebase