首页 > 解决方案 > React Error Unhandled Rejection (TypeError): api_call.json 不是一个函数。拿来?异步?

问题描述

我在一个天气应用程序(我是初学者)的项目上工作,当我单击按钮获取天气时,它不起作用。

它告诉我未处理的拒绝(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",
    });
  }
};

标签: javascriptreactjsfetchasync.js

解决方案


推荐阅读