Variable 'newData' is used before being assigned - TypeScript

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Variable 'newData' is used before being assigned - TypeScript



I am getting error in the below implementation of typescript code. I am mapping here one type to another. But vscode shows error that variable 'newData' is used before being assigned. I know it may be a silly error, but I am unable to find it.


onKeyUp(value: string)
console.log(value);
var link = ``
link = `$API/$value`
this.hn.getNews().subscribe(data =>
this.loading = false;
var newData:NewsItem;
fetch(link)
.then(function(response)
return response.json();
)
.then(function(myJson)
for (var i = 0; i < myJson.length; i++)
newData.push(myJson[i])

)
this.news = newData;
);

}




1 Answer
1



You need to initialize it to empty array as follows,


var newData:NewsItem = ;





thank you so much. I can't believe I missed that. Thank you so much. Have a nice day, sir!
– CodeNinja
Aug 12 at 2:09






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS