【问题标题】:Chain requests so I can use data from my first request in my second one链接请求,以便我可以在第二个请求中使用第一个请求中的数据
【发布时间】:2026-02-01 03:30:01
【问题描述】:

我刚刚开始了一个简单的天气应用项目来训练 React 和数据获取。

import React, { Component } from 'react';
import './App.css';

import axios from 'axios';


class App extends Component {

  constructor(props) {
    super(props);

    this.state = {
      city: "",
      id: 0
    }

    this.choseCity = this.choseCity.bind(this);
    this.cityName = this.cityName.bind(this);
    this.cityInfo = this.cityInfo.bind(this);
  }


  chooseCity(e) {
    console.log(e.target.value)
    this.setState({
      city: e.target.value
    });
  }


  cityName() {
    axios.get(`https://www.metaweather.com/api/location/search/?query=${this.state.city}`)
      .then(res => res.data)
      .then(data => this.setState({
        city: data[0].title,
        id: data[0].woeid}))
  }

  cityInfo() {
    axios.get(`https://www.metaweather.com/api/location/${this.state.id}/`)
      .then(res => console.log(res.data))
  }


  render() {
    return (
      <div className="App">
        <input type="text" placeholder="Enter city name" value={this.state.city} onChange={this.chooseCity}/>
        <button onClick={this.cityName}>Name</button>
        <button onClick={this.cityInfo}>Info</button>

      </div>
    );
  }
}

export default App;

所以,我有 2 个函数(cityName 和 cityInfo),我可以在 2 个不同的 onClick 事件上执行它们。他们似乎都独立工作。

  • cityName() 请求存储在我所在州的数据。

  • cityInfo() 在请求的 url 中使用此状态来获取更多信息。

我正在尝试链接它们以便能够在一次调用中检索所有数据,但由于它们都是异步的,所以我的第二个请求在第一个请求的数据存储在我的状态之前开始,并且有没有办法在 api 中我可以直接在一个请求中获取我需要的信息。

我已经尝试了一些方法,比如将它们分组到一个函数中,但到目前为止还没有定论。

解决方案:@elsyr

这是如何使用请求之间的数据链接到一个函数:

cityInfo() {
    axios.get(`https://www.metaweather.com/api/location/search/?query=${this.state.city}`)
      .then(res => res.data)
      .then(data => {
        axios.get('https://www.metaweather.com/api/location/' + data[0].woeid)
          .then(res => res.data)
          .then (data => this.setState({
            info: data
          }))
      });
  }

【问题讨论】:

标签: javascript reactjs httprequest axios


【解决方案1】:

据我所知,您想在cityName 中设置状态后立即触发cityInfo 中的调用。

这是完全可能的 - setState 是异步的(您似乎已经想通了),但 setState 也有一个可选的 callback parameter 可供您使用。 保证回调状态被修改后触发。

您的代码在cityName 中可能如下所示:

cityName() {
    axios.get(`https://www.metaweather.com/api/location/search/?query=${this.state.city}`)
      .then(res => res.data)
      .then(data => this.setState({
        city: data[0].title,
        id: data[0].woeid}),
        this.cityInfo); // callback here! runs after the state is set!
 }

您的另一个选择是将您的axios 调用链接在一起。如果我们在这里查看您的代码块:

cityName() {
    axios.get(`https://www.metaweather.com/api/location/search/?query=${this.state.city}`)
      .then(res => res.data)
      .then(data => {
          // data here already contains the id - data[0].woeid
          // we can just use it here to kick off another request - something like:
          // axios.get('https://www.metaweather.com/api/location/' + data[0].woeid)
      });
  }

因为您使用了.then(),所以您知道在开始您的cityInfo 通话之前您一定会获得您的数据。这可能需要更多的重构,但如果您只保留cityName 的状态以在cityInfo 中使用它,这可能是一个更好的主意。

【讨论】:

  • 第二个解决方案完美!我已经尝试过像这样链接 axios 请求,但我没有使用第一个请求提供的数据。谢谢!
最近更新 更多