This is a common gotcha among js beginners, basically the usage of the this
keyword. The context of the this
variable inside the anonymous function is not the one you intended (in this case, the element). To solve this, you can use closures[1], .bind
[2], or the newer ES2015 arrow functions[3].
Closure
ready: function() {
// assign this to self
var self = this;
var leserbriefRef = firebase.database().ref('leserbriefe');
leserbriefRef.on('value', function(snap) {
var n = snap.child('numLeserbriefe').val();
console.log(n);
self.lbriefe = [];
for(var i = 0; i < n; i++){
self.push("lbriefe", snap.child('l'+(n - 1 - i)).val());
}
});
}
.bind
ready: function() {
var leserbriefRef = firebase.database().ref('leserbriefe');
leserbriefRef.on('value', function(snap) {
var n = snap.child('numLeserbriefe').val();
console.log(n);
this.lbriefe = [];
for(var i = 0; i < n; i++){
this.push("lbriefe", snap.child('l'+(n - 1 - i)).val());
}
}.bind(this)); // bind this keyword to element's
}
ES2015 arrow function (might as well go full ES2015)
ready() {
const leserbriefRef = firebase.database().ref('leserbriefe');
leserbriefRef.on('value', (snap) => { // arrow functions!
const n = snap.child('numLeserbriefe').val();
console.log(n);
this.lbriefe = [];
for(let i = 0; i < n; i++){
this.push("lbriefe", snap.child(`l${n - 1 - i}`).val());
}
});
}
Sources:
[1] https://developer.mozilla.org/en/docs/Web/JavaScript/Closures
[2] https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind
[3] https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions