试图返回weatherMsg,它是从对天气API 的请求创建的字符串。如控制台日志所示,它正确请求天气。return 是 '' 的初始值。class Weather extends React.Component { render() { let request = require('request'); let apiKey = process.env.REACT_APP_WEATHER_API_KEY; let city = process.env.REACT_APP_WEATHER_CITY; let url = `http://api.openweathermap.org/data/2.5/weather?id=${city}&units=imperial&appid=${apiKey}` let weatherMsg = ''; request(url, function (err, response, body) { if(err){ console.log('error retrieving weather'); } else { let weather = JSON.parse(body); console.log(weather); weatherMsg = "It's {this.state.weather.main.temp} degrees in {this.state.weather.name}."; } }); return( <div>{weatherMsg}</div> ) }}
添加回答
举报
0/150
提交
取消