4

I am new to Angular2 and here I am trying to loop through the array "mmEditorTypes" and checking the condition, if the condition is satisfied then I'll be executing the "open method".

But whenever i execute the below code, I am getting this error :

portalModeService: loading of portalMode threw exception:

TypeError: Cannot read property 'forEach' of undefined".

Could someone let me know how to fix this error?

porta.service.ts :

function isAppContained(viewState, appType){
      let angular:any;
      let isContained = false;
      angular.forEach(viewState.appViewStates, function (appViewState) {
        if (appViewState.signature.appType === appType) {
          isContained = true;
        }
      });
      return isContained;
    }
1
  • The question is answered but actually the error is actually because you have not initialised angular. Commented Apr 2, 2020 at 18:11

2 Answers 2

11

As @Sajeetharan said you can't use angular.forEach in angular 2+

So you can use simple foreach in typescript like :

var someArray = [1, 2, 3];
someArray.forEach((item, index) => {
  console.log(item); // 1, 2, 3
  console.log(index); // 0, 1, 2
});
Sign up to request clarification or add additional context in comments.

Comments

5

You cannot use angular.forEach with angular , its with angularjs.

use

for (let appViewState of viewState.appViewStates) {
   if (appViewState.signature.appType === appType) {
          isContained = true;
    }
}

3 Comments

Assuming viewState.appViewStates is an array, you can also use Array.some
Thank you @Sajeetharan , Now the error is resolved after replacing it with your code , But when i am executing this code , i am getting TypeError: Cannot read property 'length' of undefined error : for(let editor of mmEditorTypes){ // mmEditorTypes.forEach(function(editor) { if (!isAppContained(viewState, editor.type)) { this.applicationLifeCycleService.open(editor.type, editor.payload); } } could you please let me know if i am doing anything wrong here ?.
i dont see anywhere you use length in the code yu provided

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.