在反应中使用 Promise.all 时出错



我一直在使用axios.get来处理我的api请求,但我正在测试如何在componentDidMount上拉取多个api请求。 我一直在查找并使用其他人的代码,并决定 Promise.all 是要走的路。 我绝对可能是错的,不介意为此回到 axios。 使用 axios.spread 方法抛出了一个错误,Promise.all 也抛出了同样的错误。 关于为什么这不起作用的任何想法?

import React from "react";
import ReactDOM from "react-dom";
import axios from 'axios'
import { Table } from './Table'
export class DataList extends React.Component {
state = {
articles: [],
google: []
}
componentDidMount() {
Promise.all([
('http://127.0.0.1:8000/api/portblog/'),
('http://www.google.com')
])
.then(([res, googleRes]) => {
this.setState({
articles: res.data,
google: googleRes
})
console.log(res.data)
})
}
render() {
return(
<div>
<Table key={this.state.articles.id} 
articles={this.state.articles} />
</div>
)
}
}
export default DataList

编辑:错误:

DataList.js?a7e0:29 Uncaught TypeError: Cannot read property 'id' of undefined

试试这个。我没有看到您向这些 URL 发送 ajax 请求。

Promise.all([
axios.get('http://127.0.0.1:8000/api/portblog/'),
axios.get('http://www.google.com')
])

最新更新