[英]How to combine responses from two calls in one response?
我得到一個空響應 ( {}
),而我預期的響應格式為:
{
locationResponse: "location foo",
forecastResponse: "forecast bar"
}
在我的索引文件中,我有:
const {getCity} = require('./routes/city');
const {getForecasts} = require('./routes/forecast');
app.get('/forecasts', function (req, res) {
var location = getCity(req, res);
var forecast = getForecasts(req, res);
//these are logged as undefined
console.log("Inside index.js");
console.log(location);
console.log(forecast);
res.send({locationResponse: location, forecastResponse: forecast});
});
在預測文件中,我有以下內容,城市文件中有一個類似的:
module.exports = {
getForecasts: (req, res) => {
var result = //mySQL DB calls and processing
console.log("Inside getForecasts");
console.log(result); //actual result printed
return "Forecast";
}
更新:所以我在每次調用的返回語句之前添加了一些日志,並發現日志按以下順序打印,這意味着它沒有按預期工作,因為我沒有考慮到它們是異步調用的事實。
Inside index.js
undefined
undefined
Inside getForecasts
{result}
這里的問題是,在你的./routes/forecast/
getForecasts 方法中,你告訴響應發送,數據“預測”。 每個請求只應使用 res.send 一次,因為這將解析響應並返回給客戶端。
相反,您的 getForecasts 方法應該只返回您需要的任何數據,並且您的索引文件應該處理響應。 如果您也需要 getForecasts 來處理響應,也許是因為您將請求直接發送到不需要位置數據的預測端點,那么您可以重構您的代碼,以便索引和預測都調用以獲取您的數據需要。 例如:
/* index.js */
const {getCity} = require('./data/city');
const {getForecasts} = require('./data/forecast');
app.get('/forecasts', function (req, res) {
var location = getCity();
var forecast = getForecasts();
res.send({locationResponse: location, forecastResponse: forecast});
});
/* data/forecast.js */
module.exports = {
getForecasts: () => {
return "Forecast";
}
};
/* data/city.js */
module.exports = {
getCity: () => {
return "City";
}
};
然后你還可以有:
/* routes/forecast.js */
const {getForecasts} = require('../data/forecast');
module.exports = {
getForecasts: (req, res) => {
res.send(getForecasts());
}
};
上面的內容可能過於復雜,但我假設如果您使用routes
目錄,您可能希望將路由處理程序存儲在那里。 希望這可以幫助。
似乎您的 getCity() 和 getForecasts() 函數都是異步的。 這些異步函數返回一個承諾而不是實際的響應。
所以你可以使用簡單的asysn/await或者 JS 中的Promise.all來解決這個問題。
選項 1:在將消息記錄到控制台之前,使用await
作為要解決的承諾:
app.get('/forecasts', async function (req, res) {
var location = await getCity(req, res);
var forecast = await getForecasts(req, res);
//these are logged as undefined
console.log("Inside index.js");
console.log(location);
console.log(forecast);
res.send({locationResponse: location, forecastResponse: forecast});
});
選項 2:使用Promise.all()
等待所有Promise.all()
完成。
app.get('/forecasts', function (req, res) {
var list = await Promise.all([getCity(req, res), getForecasts(req, res)]);
//these are logged as undefined
console.log("Inside index.js");
console.log(list[0]);
console.log(list[1]);
res.send({locationResponse: list[0], forecastResponse: list[1]});
});
您可以使用async/await
語法。
app.get('/forecasts', async function (req, res) {
var location = await getCity(req, res);
var forecast = await getForecasts(req, res);
//these are logged as undefined
console.log("Inside index.js");
console.log(location);
console.log(forecast);
res.send({locationResponse: location, forecastResponse: forecast});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.