I have this ES6 class, which fails when this.categories = data.data
is executed with the error TypeError: Cannot set property 'categories' of undefined
.
I think it is due to this
refering to the context inside the promise.
How do I set the this.categories
from inside the promise?
Thanks.
class NavbarController {
constructor(bnCategoryService) {
this.categories = []; // This is what should be set with data.data.
this.bnCategoryService = bnCategoryService;
this.getCategories();
}
getCategories() {
this.bnCategoryService.getCategories() // Returns a promise
.success(function(data, status, headers, config) {
console.log(data.data);
this.categories = data.data; // This fails!
})
.error(function(data, status, headers, config) {
console.log('Category service: An error occured getting tags from the server.');
});
}
}