当我访问(由于缺乏更好的术语)'/postUserInput' POST路由时,它要么不运行getGeoNamesData(),直到结束,或者它不等待API获取完成。我从这个函数得到的数据用于同时调用API。
我应该做些什么来确保这些函数按顺序运行?
这是我的服务器文件代码。它按照控制台的顺序运行:第一第三第四第五第六第七八第二个
这使所有剩余的API调用都无法获得正确的数据。请帮助。
谢谢。
app.post('/postUserInput', function (req, res) {
currentWeatherUI = req.body.buildCurrentBoolean;
userInput = {
userCityInput: req.body.userCityInput,
userStateInput: req.body.userStateInput,
userDateInput: req.body.userDateInput,
};
console.log("first");
getGeoNamesData();
console.log("third");
if (currentWeatherUI) {
getCurrentWeatherbitData(longitude, latitude);
console.log("fifth");
} else {
getFutureWeatherbitData(longitude, latitude);
};
console.log("sixth");
getPixabayData();
console.log("eigth");
});
const getGeoNamesData = async () => {
try{
const response = await axios.get('http://api.geonames.org/searchJSON?q='+userInput.userCityInput+'+'+userInput.userStateInput+'&maxRows=1&username='+geonamesUser);
longitude = response.data.geonames[0].lng
latitude = response.data.geonames[0].lat
console.log("second");
} catch(error) {
console.log("error2", error);
};
};
const getCurrentWeatherbitData = async (longitude, latitude) => {
try {
const weatherbitURL = "http://api.weatherbit.io/v2.0/current?&lat="+latitude+"&lon="+longitude+"&key="+weatherbitUser+"&units=I&lang=en";
const response = await axios.get(weatherbitURL);
let newDayObject = {
city: response.data.data[0].city_name,
state: response.data.data[0].state_code,
date: userInput.userDateInput,
temp: response.data.data[0].temp,
wind: response.data.data[0].wind_spd,
windDirection: response.data.data[0].wind_cdir,
rain: response.data.data[0].precip,
snow: response.data.data[0].snow
};
projectDataArray.push(newDayObject);
console.log("fourth");
} catch(error) {
console.log("error", error);
};
};
const getPixabayData = async () => {
try {
const pixabayURL = "https://pixabay.com/api/?key="+pixabayUser+"&q="+userInput.userCityInput+"+"+userInput.userStateInput+"&image_type=photo&pretty=true&per_page=3";
const response = await axios.get(pixabayURL);
if (response.data.hits.length > 0) {
let cityImageObj = {cityImageURL: response.data.hits[0].webformatURL};
projectDataArray.push(cityImageObj);
} else {
let cityImageObj = {cityImageURL: "https://www.pennlive.com/resizer/vNu0aYjk3xlFTUb16FSrSji_DIA=/1280x0/smart/advancelocal-adapter-image-uploads.s3.amazonaws.com/image.pennlive.com/home/penn-media/width2048/img/life/photo/wintermeme11.jpg"};
projectDataArray.push(cityImageObj);
}
console.log("seventh");
} catch(error) {
console.log("error", error);
};
};```
请查看MDN文档中的Javascript异步函数。根据文档:
Async函数可以包含0个或多个await表达式。Await表达式通过暂停执行,直到返回的承诺被完成或拒绝,使返回承诺的函数表现得好像是同步的。
这应该运行函数- getGeoNamesData, getCurrentWeatherbitData getPixabayData按您期望的顺序。
app.post("/postUserInput", async function (req, res) {
currentWeatherUI = req.body.buildCurrentBoolean;
userInput = {
userCityInput: req.body.userCityInput,
userStateInput: req.body.userStateInput,
userDateInput: req.body.userDateInput,
};
console.log("first");
await getGeoNamesData();
console.log("third");
if (currentWeatherUI) {
await getCurrentWeatherbitData(longitude, latitude);
console.log("fifth");
} else {
await getFutureWeatherbitData(longitude, latitude);
}
console.log("sixth");
await getPixabayData();
console.log("eigth");
});