im new to ember js, and below is my code for create tags array from the selectedEntities array. its successfulyy console logs the values inside the selectedEntities array, but when adding created object (tag) values to tags array its always gives "Cannot read property 'tags' of undefined". How to solve this.
export default class Merchants extends Controller.extend(DebounceQueryParams) {
tags= A([]);
selectedEntities = A([]);
@action
openTestModal() {
this.selectedEntities.forEach(function (e){
console.log("name ", e.contactInfo.contactName);
console.log("e.id ", e.id);
if(e.workflowTask !==null){
console.log("e.workflowTask.currentStatus ", e.workflowTask.currentStatus);
const tag = {
id: e.id,
name: e.contactInfo.contactName,
status: e.workflowTask.currentStatus
};
this.tags.pushObject(tag);
}
const tag = {
id: e.id,
name: e.contactInfo.contactName,
status: e.workflowTask.currentStatus
};
this.tags.pushObject(tag);
});
this.remodal.open('user-assign');
}
}
That's because you're using a function
keyword in the forEach call. When you do so it has its own this
and surely this this
doesn't have any tags in it. Either use arrow function, i.e. .forEach(e => {
or save the outer this
into a variable:
openTestModal() {
const self = this;
this.selectedEntities.forEach(function (e){
...
self.tags.pushObject(tag);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments