javascript - 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",
});
}
};
解决方案
推荐阅读
- javascript - 单击后具有唯一值的数据表列过滤器多选
- net-snmp - 当我使用 snmp 时,如何解决“此 OID 的此代理上没有可用的此类对象”?
- python - MDCARD 未在单击时删除
- proto.actor - 如何在 C# 中定义的序列化程序中注册消息
- swagger - 用于字段的 ApiParam
- swift - 如何在同一个窗口中转换到新的视图控制器?(斯威夫特/故事板,macOS)
- java - 使用@Slf4j 注解进行日志记录
- python - BeautifulSoup - AttributeError: 'NoneType' 对象没有属性 'findAll' Webscraping a blog with links
- java - Java手动添加lib,没有IDE
- python - 有没有办法绕过匿名服务检查?