Ошибка реагирования Необработанный отказ (TypeError): api_call.json не является функцией. Fetch? асинхронный? - PullRequest
0 голосов
/ 26 апреля 2018

Я работал над проектом для погодного приложения (я новичок), и когда я нажимал на кнопку, чтобы получить погоду, она не работает.

Это говорит мне о необработанном отклонении (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",
    });
  }
};
...