React + Fetch+ Json. TypeError: Cannot read property of undefined
React + Fetch+ Json. TypeError: Cannot read property of undefined
当我使用 SuperAgent 时我没有遇到这个问题,但是我决定使用 Window.fetch polifyl 并且我遇到了这个问题。我看到所有数据都已加载,但仍然显示错误。
你能帮我找出这个错误吗:
在 render() 中,我根据获得的列表生成了一个组件列表:
render() {
if (this.state.data.list) {
console.log("render: " + this.state.data.list);
var counter = 0;
const list = this.state.data.list.map((item) => {
....
您的屏幕截图中的承诺处理程序将不起作用:
.then((json) => console.log('parsed json: ', json))
.then((json) => { this.setState({ data: json }); })
"Take the value from resolving this promise and pass it to console.log
. Then, take console.log's return value (which is undefined
) and pass it to this.setState
."
fetch(url, {
headers: {
'Accept': 'application/json',
},
}).then((response) => response.json()
.catch(err => {
console.err(`'${err}' happened!`);
return {};
}))
.then((json) => {
console.log('parsed json: ', json);
this.setState({ data: json })
})
.catch((err) => { console.log('fetch request failed: ', err) }
)
当我使用 SuperAgent 时我没有遇到这个问题,但是我决定使用 Window.fetch polifyl 并且我遇到了这个问题。我看到所有数据都已加载,但仍然显示错误。
你能帮我找出这个错误吗:
在 render() 中,我根据获得的列表生成了一个组件列表:
render() {
if (this.state.data.list) {
console.log("render: " + this.state.data.list);
var counter = 0;
const list = this.state.data.list.map((item) => {
....
您的屏幕截图中的承诺处理程序将不起作用:
.then((json) => console.log('parsed json: ', json))
.then((json) => { this.setState({ data: json }); })
"Take the value from resolving this promise and pass it to console.log
. Then, take console.log's return value (which is undefined
) and pass it to this.setState
."
fetch(url, {
headers: {
'Accept': 'application/json',
},
}).then((response) => response.json()
.catch(err => {
console.err(`'${err}' happened!`);
return {};
}))
.then((json) => {
console.log('parsed json: ', json);
this.setState({ data: json })
})
.catch((err) => { console.log('fetch request failed: ', err) }
)