node.js - 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;
});
}
}
解决方案
您需要将其初始化为空数组,如下所示,
var newData:NewsItem[] = [];
推荐阅读
- linux - 如何在 Linux 来宾 VM 中运行 Hyperv Socket
- google-sheets - 来自谷歌表格的 Highmaps 数据不起作用
- java - Hashmap - 返回键并将其设置为对象的值
- c# - 在 GitHub 操作上找不到程序集“Windows”
- c# - 在 ASP.NET Framework 中的服务器发送事件中针对流存储 SessionID
- javascript - 容器的响应式背景图像
- java - 如何解决 java.lang.StackOverflowError?
- linux-kernel - mmap:映射一个 ARM linux 内核以进行仿真
- git - 如何从我的 git 本地分支中明确删除文件?
- java - 为什么我的 Hibernate `select` 查询超级慢?