我正在尝试用console.log记录天气API的一些数据,但是当我去查找位置时会收到错误消息
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). (rejection id: 4)
到目前为止,我的代码在我的服务器上
app.post('/weather',(req,res) => {
const url = `https://api.darksky.net/forecast/${DARKSKY_API_KEY}/${req.body.latitude},${req.body.longitude}?units=auto`
axios({
url: url,responseType: 'json'
}).then(data => res.json(data.data.currently)).catch(error => { throw error})
})
app.listen(3000,() => {
console.log("Server has started")
})
和我的Javascript
fetch('/weather',{
method: 'POST',headers: {
'Content-Type': 'application/json','accept': 'application/json'
},body: JSON.stringify({
latitude: latitude,longitude: longitude
})
}).then(res => res.json()).then(data => {
console.log(data)
setWeatherData(data,place.formatted_address)
}).catch(error => { throw error})
})