[英]Expressjs - Is there a way to have helper function with req,res object
[英]ExpressJS creating multiple JSON objects with helper function
我正在嘗試使用具有不同查詢參數的 FourSquare API 的外部調用來創建多個 JSON 對象,但我的代碼一直作為成功的響應代碼返回,沒有可讀的結果。 這是我嘗試使用的代碼:
async function getPlaces(query, lat, lon, clientID, clientSecret, versionDate) {
const URL = `https://api.foursquare.com/v2/venues/search?client_id=${clientID}&v=${versionDate}&ll=${lat},${lon}&intent=browse&radius=10000&query=${query}&limit=10&client_secret=${clientSecret}`;
const response = await fetch(URL).catch(e => { console.log(e) });
const data = await response.json().catch(e => { console.log(e) });
return data;
}
當我調用這個 function 來創建 JSON object 像這樣
const beachData = getPlaces("beaches", lat, lon, clientID, clientSecret, versionDate);
然后它返回如下:
Promise { <pending> }
我嘗試了同樣的結果
const beachData = getPlaces("beaches", lat, lon, fourSquareClientID, fourSquareClientSecret, versionDate).then(({ beachData }) => { beachData; });
任何關於我做錯了什么的指導將不勝感激。 如果我沒有完全偏離主題,我希望能夠多次調用這個 function 並獲取一些 JSON 對象,我稍后可以將響應組合回前端。 謝謝!
如果您嘗試記錄fetch
的返回值;
let x = fetch(URL);
console.log(x);
你會得到:
Promise { <pending> }
所以解決方案當然是await
它:
let x = await fetch(URL);
同樣,您需要await
您的 function:
const beachData = await getPlaces("beaches", lat, lon, clientID, clientSecret, versionDate);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.