Skip to content
Advertisement

Unable to accessible some code in TypeScript TS

I have a simple if else code in TypeScript.But in some code unable to access it.It shows me the following error,

“Cannot read properties of undefined (reading ‘setNewsProvider’)”

Code

JavaScript

Advertisement

Answer

Inside the forEach loop you enter a function, the function has it’s own this value. To avoid the problem, JS programmers used to often write

JavaScript

at the entry point and then use that.setNewsProvider() so that a more locally scoped this does not override it. if you use an arrow function the problem will be avoided as these do not have their own local this value.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement