我在nodejs上有一个端点,虽然它是一个承诺,但它不会产生输出,它应该返回输出。这是我的要求:
TegucigalpaData: () => {
let url = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent('Tegucigalpa, Honduras')}`;
axios.get(url).then( (response) => {
if(response.data.status === 'ZERO_RESULTS') {
throw new Error(`Unable to find the address: ${address}`);
}
let lat = response.data.results[0].geometry.location.lat;
let lng = response.data.results[0].geometry.location.lng;
let wurl = `https://api.darksky.net/forecast/dc6c0d767d55ffe8f172d4c187852db8/${lat},${lng}?lang=es&units=si`;
return axios.get(wurl);
}).then( (response) => {
let temperature = response.data.currently.temperature;
let apparentTemperature = response.data.currently.apparentTemperature;
return { temp: temperature, appTemp: apparentTemperature }
}).catch( (error) => {
return "Unable to connect to the API servers";
});
}
然后,在我的终端上:
app.get('/weatherData', (req, res) => {
let data = TegucigalpaData();
res.send(data);
});
但是,我没有像json那样得到任何数据输出。知道我可能做错了什么吗?
答案 0 :(得分:0)
尝试直接在处理程序中发送结果:
app.get('/weatherData', (req, res) => {
let url = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent('Tegucigalpa, Honduras')}`;
axios.get(url).then( (response) => {
if(response.data.status === 'ZERO_RESULTS') {
throw new Error(`Unable to find the address: ${address}`);
}
let lat = response.data.results[0].geometry.location.lat;
let lng = response.data.results[0].geometry.location.lng;
let wurl = `https://api.darksky.net/forecast/dc6c0d767d55ffe8f172d4c187852db8/${lat},${lng}?lang=es&units=si`;
return axios.get(wurl);
}).then( (response) => {
let temperature = response.data.currently.temperature;
let apparentTemperature = response.data.currently.apparentTemperature;
return res.send({ temp: temperature, appTemp:
apparentTemperature })
}).catch( (error) => {
return res.send(error)
});
}
});