I am trying to create a Student Attendance Tracker Application with javascript with firebase.
But i have a problem, I am trying to query inside a function and have the function return those data.
But when i try, it returns nothing. 'undefined'
Here is my code
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;
};
}
And here is my code to test it
var db = initFireSubjects();
var test = db.getAll('2014');
console.log(test);
The structure of the data looks like this
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'
}
}
}
The case is i can pull data if i do it like this
db.getAll = function(year){
var value;
this.connection.child(year).on('value',function(snapshot){
value = snapshot.val();
console.log(value);
}
}
I don't know why it can't pull data in an object oriented manner.
Any input?
Thanks.