Cannot read property 'kind' of undefined in enger JS when adding objects to array

dasitha

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');
      }
}
Andrey Stukalin

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.

edited at
0

Comments

0 comments
Login to comment

Related

Angular 8 :: TypeError: Cannot read property 'kind' of undefined, When making build for Production

Cannot read property Array of undefined Vue

"Cannot read property 'id' of undefined" when parsing an array declared in a factory

Leaflet throwing "Uncaught TypeError: Cannot read property 'lat' of undefined" when adding new marker to map

Looping through array of objects - TypeError: Cannot read property 'name' of undefined

ReactJS "TypeError: Cannot read property 'array' of undefined"

Cannot read property 'filter' of undefined Array in Angular

ng build error in cannot read property 'kind' of undefined

array of objects: Cannot read property 'id' of undefined inside onchange event

TypeError: Cannot read property 'string' of undefined when adding google-tag-manager to reactjs site

Adding an object to the array React; "TypeError: Cannot read property 'push' of undefined"

Angular cli build prod: TypeError: Cannot read property 'kind' of undefined

Angular - Cannot read property 'kind' of undefined at isAngularDecoratorMetadataExpression

Cannot read property 'cards' of undefined in @lourenci/react-kanban when adding a card

Uncaught TypeError: Cannot read property '__e3_' of undefined when Adding GetJson on Initialize()

Cannot read property 'push' of undefined on array

getting a cannot read property of undefined error from vue.js when using v-model with an array

.map on an array of objects within another array: TypeError: Cannot read property 'map' of undefined

angular 5 array Cannot read property ' ' of undefined

Cannot read property 'push' of undefined on an Array of objects (Typeof shows object)

TypeError: Cannot read property 'value' of undefined When adding a value to an Array with Redux

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined when adding object to field in Sharepoint

TypeError: Cannot read property 'x' of undefined when comparing objects in an array in React

Cannot read property 'username' of undefined when array reaches the end of iteration

"Cannot read property 'length' of undefined" on an empty array

Getting 'Cannot read property of undefined' when accessing an array object

Vue.js "cannot read property 'undefined' of undefined" when accessing array of objects using Vue variable as index

JS Array map function, cannot read property of undefined

Cannot read property 'push' of undefined in empty array