发送到客户端错误后无法设置标头(节点/快速菜鸟,其他解决方案似乎不起作用或不理解它们))



我看了其他有类似问题的人的答案,但我对这一切仍然有点陌生,所以也许我没有正确理解或实现类似问题的解决方案。

这是一个Node/Express应用程序,我正在尝试练习,我使用API从搜索字段获取经度和纬度,然后使用该API响应数据使用另一个API,使用经度和纬度来获取天气数据。我还有另一个应用,我静态编码了经纬度,一切正常。但是,我确实有嵌套的API调用,并且post请求是新的。

我得到这个错误,虽然,"错误[ERR_HTTP_HEADERS_SENT]:不能设置头后,他们被发送到客户端"当我尝试渲染视图时。当我测试并删除第二个嵌套API调用时,它工作了,或者如果我取出第二个res.render,它似乎工作了。

我不知道是我所做的是不可能的,还是像我说的,人们也有类似的问题,我只是没有正确地实施任何解决方案。

理想情况下,我需要从嵌套的API调用中获取数据,从"weatherDataResponse"获取数据到res.render视图。我确实让它自己工作,但是当我引入/post和顶层API调用时,它不起作用。

代码:

function latLongInfo(results) {
let obj = {
method: 'GET',
url: 'https://forward-reverse-geocoding.p.rapidapi.com/v1/forward',
params: {
street: '',
city: results.city,
state: results.state,
postalcode: results.zip,
country: 'USA',
'accept-language': 'en',
polygon_threshold: '0.0'
},
headers: {
'x-rapidapi-key': rapidApiKey,
'x-rapidapi-host': 'forward-reverse-geocoding.p.rapidapi.com'
}
}
return obj
};
app.get('/', function(req, res) {
res.render('index', { title: 'idk' });
});
app.post('/getweather', (req, res, next) => {
const cityWeatherSearch = req.body.cityWeatherSearch
next();
axios.request(latLongInfo(parseLatLongSearch(cityWeatherSearch)))
.then(function (response) {
const getLatLongInfoResponse = response.data[0]
console.log(getLatLongInfoResponse)
if (Object.keys(getLatLongInfoResponse).length < 1) {

res.render('invalidweather', { title: 'Invalid Results' });
}
else {
let lat = getLatLongInfoResponse.lat, lon = getLatLongInfoResponse.lon;
let latLonString = `${lat},${lon}`;
const getWeatherInfo = axios.get(`https://api.pirateweather.net/forecast/${pirateWeatherKey}/${latLonString}`)
axios.all([getWeatherInfo]).then(axios.spread((...responses) => {
const weatherDataResponse = responses[0].data;
console.log(weatherDataResponse)
res.render('weather', { title: 'Weather Results' });
})).catch(errors => {
console.error(errors);
})
}
})
.catch(function (error) {
console.log(error)
})  
res.end()

})

试试这样做。看起来是next()调用导致了这里的问题。我还重构了一些异步的东西,这样它更容易读。

function latLongInfo(results) {
let obj = {
method: 'GET',
url: 'https://forward-reverse-geocoding.p.rapidapi.com/v1/forward',
params: {
street: '',
city: results.city,
state: results.state,
postalcode: results.zip,
country: 'USA',
'accept-language': 'en',
polygon_threshold: '0.0'
},
headers: {
'x-rapidapi-key': rapidApiKey,
'x-rapidapi-host': 'forward-reverse-geocoding.p.rapidapi.com'
}
}
return obj
};
app.get('/', function(req, res) {
res.render('index', { title: 'idk' });
});
app.post('/getweather', async (req, res, next) => {
try {
const cityWeatherSearch = req.body.cityWeatherSearch;
const response = await axios.request(latLongInfo(parseLatLongSearch(cityWeatherSearch)));
const getLatLongInfoResponse = response.data[0]
console.log(getLatLongInfoResponse)
if (Object.keys(getLatLongInfoResponse).length < 1) {
res.render('invalidweather', { title: 'Invalid Results' });
return;
}
const lat = getLatLongInfoResponse.lat;
const lon = getLatLongInfoResponse.lon;
const latLonString = `${lat},${lon}`;
const getWeatherInfo = await axios.get(`https://api.pirateweather.net/forecast/${pirateWeatherKey}/${latLonString}`);
const weatherDataResponse = getWeatherInfo.data;
console.log(weatherDataResponse)
res.render('weather', { title: 'Weather Results' });
} catch (error) {
console.log(error);
}
});

相关内容

最新更新