javascript - 无法访问数组中的数据,name[0] 未定义
问题描述
我正在尝试从调用fetch
.
当我打印数据时,例如console.log(data)
,我得到了我想要的结果。但是,当我尝试打印第一个项目时,例如console.log(data[0])
,我看到了undefined
。
componentDidMount = () => {
fetch("http://localhost/system_reklamacji/php/pobierz_pliki.php", {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
'klucz_nr': this.state.id,
})
}).then(res => this.KonkretnePliki(res.json()));
以及发生错误的函数:
KonkretnePliki(e) {
let pliki = [];
(e.then(function(value) {
value.forEach(element => {
console.log(element[0]);
let temp = {
'nazwa': element[0],
'sciezka': element[1],
'typ': element[1].slice(element[1].length - 3, ),
};
console.log(pliki[0]); // **shows all the information i want**
pliki.push(temp);
})
}));
console.log(pliki[0]); // **shows undefined**
this.setState({
dane: pliki
});
}
有什么方法可以让我在forEach
循环之外获取所需的数据?
解决方案
推荐阅读
- r - 如何根据其他矩阵的行名对矩阵进行子集化和重新排序
- php - Laravel 更新 OneToOne 雄辩关系
- flutter - 用于颤振的 Windows 的 iOS 模拟器
- django - Heroku Redis - 什么是连接?Python Celery 完成任务后,为什么我的连接不会降为零?
- javascript - 如何使用 momentJS 格式化短日期?
- c# - 将字符串中的特殊字符更改为空格,然后获取原始字符串
- javascript - 有没有办法找到代码在浏览器上创建动态弹出窗口的位置?
- github - 使用 GitPython 克隆存储库显示“无法预加载包文件:没有此类文件或目录错误”
- webpack - 删除 webpack 离线插件的正确方法是什么?
- c# - Net Core 3.1 BackgroundService 导出图