如何在React.js中的处理函数中调用Promise.all

时间:2018-06-12 20:27:26

标签: javascript reactjs es6-promise

我尝试根据输入进行两次API调用;

api.js

import axios from 'axios';

export const getWeather = input => {
};

export const getForecast = input => {
};

在我的React组件中:

import React, { Component } from 'react';
import WeatherDisplay from './WeatherDisplay';
import * as api from '../utils/api';
import dataTracker from '../utils/dataTracker';

import '../scss/app.scss';

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      input: '',
      weatherFromInput: null,
    };
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  componentDidMount() {
    console.dir(dataTracker);
  }

  handleChange(event) {
    this.setState({
      input: event.target.value,
    });
  }

  // prettier-ignore
  handleSubmit(event) {
    event.preventDefault();
    var promises = Promise.all(api.getWeather(this.state.input), api.getForecast(this.state.input))

    promises.then(function(input) {
      this.setState({ weatherFromInput: input[0], input: '' });
      console.log("input", input[0]);
    }.bind(this));
  }

  render() {
    return (
      <div className="container">
        <div className="container">
          <form name="weatherApp" onSubmit={this.handleSubmit}>
            <h2>Open Weather App</h2>
            <div className="row">
              <div className="one-half column">
                <label htmlFor="insertMode">Insert your location</label>
                <input
                  name="zipcode"
                  className="u-full-width"
                  placeholder="please enter city or zipcode"
                  type="text"
                  autoComplete="off"
                  value={this.state.input}
                  onChange={this.handleChange}
                />
              </div>
              <div className="one-half column">
                <label htmlFor="showMin">show minimum</label>
                <input type="checkbox" />
                <label htmlFor="showMax">show maximum</label>
                <input type="checkbox" />
                <label htmlFor="showMean">show mean</label>
                <input type="checkbox" />
              </div>
            </div>
            <div className="row">
              <div className="two-half column">
                <input type="submit" value="Submit" />
              </div>
            </div>
          </form>
        </div>

        <div className="container">
          <div className="row">
            <div className="twelve columns">
              {this.state.weatherFromInput !== null ? <WeatherDisplay weather={this.state.weatherFromInput} /> : null}
            </div>
          </div>
        </div>
      </div>
    );
  }
}

我收到此错误:

App.js:77 Uncaught (in promise) TypeError: undefined is not a function

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

我相信Promise.all()需要一个数组,所以:

var promises = Promise.all(api.getWeather(this.state.input), api.getForecast(this.state.input))

应该是

var promises = Promise.all([api.getWeather(this.state.input), api.getForecast(this.state.input)])