1👍
Your problem comes with the use of function() { }
instead of arrow function in the .forEach()
method: you are losing the reference to this
(i.e. it becomes non-lexical). This means that the this
in your callback no longer refers to the VueJS component itself, but to the Window
object instead.
Therefore, changing the callback in your .forEach()
method to use the arrow function should fix the error you’re encountering:
snapshot.forEach(childSnapshot => {
this.results = childSnapshot.val();
});
Pro-tip: since your callback contains a single line, you can make it even more readable by not using curly brackets:
snapshot.forEach(childSnapshot => this.results = childSnapshot.val());
- [Vuejs]-Vue and TypeScript not reactive for string array values
- [Vuejs]-Browser redirect a Vuetify Vue.js app if IE11 or IE10
0👍
It looks like this
is not your Vue instance.
Try binding this
to your forEach
.
snapshot.forEach(function(childSnapshot){
this.results = childSnapshot.val();
}.bind(this));
- [Vuejs]-ReCAPTCHA v3 with vue-recaptcha-v3 (v1.8.0) constantly failing verification with the fetch API
- [Vuejs]-How to provide validation on a login from components in coreui
Source:stackexchange.com