React Error Unhandled Rejection(TypeError):api_call.json不是函数。取?异步?

时间:2018-04-26 18:20:35

标签: javascript reactjs fetch async.js

我参与了一个天气应用程序项目(我是初学者),当我点击按钮让天气无法正常工作时。

它告诉我Unhandled Rejection(TypeError):api_call.json不是函数

有人会知道我为什么会遇到这个错误吗?

谢谢,

import React, { Component, } from 'react';
import {Titles, Data, Weather} from "./components";
import './App.css';


class App extends Component {

  state = {
      temperature:undefined,
      city:undefined,
      country:undefined,
      humidity:undefined,
      error:undefined
  }

getWeather = async (e) => {
  e.preventDefault();
 const city = e.target.elements.city.value;
 const country = e.target.elements.country.value;

 const key="48f82148953e62c753c8862a905797b0";
 //const api_call = await fetch('http://api.openweathermap.org/data/2.5/weather?q=${city},${country}&appid=48f82148953e62c753c8862a905797b0&units=metrics');

 const api_call = ('https://api.openweathermap.org/data/2.5/weather?q=' + city + country + '&APPID=' + key + '&units=metrics');


  const data = await api_call.json();

  if (city && country){
    console.log(data);
      this.setState({
        temperature: data.main.temp,
        city: data.name,
        country: data.sys.country,
        humidity: data.main.humidity,
        description: data.weather[0].description,
        error:"",
      });

  } else {
    this.setState({
      temperature: undefined,
      city: undefined,
      country: undefined,
      humidity: undefined,
      description: undefined,
      error:"Please enter the correct name of the city and country code",
    });
  }
};

0 个答案:

没有答案