简体   繁体   English

使用axios.all()和axios.spread()“找不到变量”方法错误

[英]Using axios.all() and axios.spread() “can't find variable” method error

I am trying to create a program that runs multiple axios requests to get data for a chart, and waits for them all to finish before pulling that data into a chart. 我正在尝试创建一个程序,该程序运行多个axios请求以获取图表数据,并等待它们全部完成后再将数据拉入图表。 This is some of my current code: 这是我当前的一些代码:

minBugs() {
  return (
    axios({
      responseType: 'json',
      auth: {
        username: '<username>',
        password: '<password>'
      }
    })
    .get('<url>'));
}

normBugs() {
  return (axios({
    responseType: 'json',
    auth: {
      username: '<username>',
      password: '<password>'
    }
  }).get('<url>'));
}

componentDidMount() {
  axios.all([minBugs(), normBugs()])
    .then(axios.spread(function(a, b) {
      //manipulate data 
    }))
    .catch(message => console.log('Axios exception: ', message));
}

When I try to run it, it gives me the following error: 当我尝试运行它时,它给了我以下错误:

ReferenceError: Can't find variable: minBugs

Any ideas why? 有什么想法吗? Thank you! 谢谢!

Since the other parts of your component is missing we can't see what is going on here, but you are using componentDidMount , then this component should be a class component. 由于缺少组件的其他部分,因此我们看不到这里发生了什么,但是您正在使用componentDidMount ,那么该组件应该是一个类组件。 So, you need to use your functions with this . 因此,您需要在this使用您的函数。

componentDidMount() {
      axios.all([this.minBugs(), this.normBugs()])
      .then(axios.spread(function (a, b) { 
         //manipulate data 
      }))
      .catch(message => console.log('Axios exception: ', message));
}

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

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