简体   繁体   English

在Promise.all反应中获取错误

[英]Getting an error using Promise.all in react

I've been using axios.get for my api requests, but I was testing out how to pull multiple api requests in on componentDidMount. 我一直在使用axios.get进行api请求,但是我正在测试如何在componentDidMount上提取多个api请求。 I have been looking this up and using other peoples code, and decided that Promise.all is the way to go. 我一直在查找并使用其他人的代码,因此确定Promise.all是可行的方法。 I definitely could be wrong, and don't mind going back to axios for this. 我绝对可能是错的,并且不介意回到axios。 Using the axios.spread method was throwing an error, and Promise.all is throwing the same error as well. 使用axios.spread方法会引发错误,而Promise.all也会引发相同的错误。 Any ideads on why this isn't working? 任何关于为什么这不起作用的想法?

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

Edit: Error: 编辑:错误:

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

Try this out. 试试看 I don't see you sending ajax requests to those URLs. 我看不到您向这些URL发送Ajax请求。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM