Trying to implement this in my VueJs methods results in an error message:
this is undefined
It seems like arrow functions may not be suitable as their this
does not bind to the expected context.
Switching to a regular function still causes the same error.
My troubleshooting attempts so far
methods: {
connection(){
new elasticsearch.Client({...});
client.search({...})
.then(function (resp) {
var hits = resp.aggregations;
this.tmp = hits[1].value;
}, function (err) {
console.trace(err.message);
});
}
}
The issue lies in not being able to access the desired this
within the functions passed to .search
and .then
. How can I ensure that this
is bound to my VueJs instance so I can utilize data
, computed
, and other properties?