在此處輸入影像描述
我需要從這個 api 端點獲取資料,并將其發送給我的客戶端(React)。前端和后端之間一切正常,但我似乎無法弄清楚如何在 /dailyscores 端點中獲取資料并使用 axios 發送它。關于為什么 res.send 不是 .then 中的函式以及使其作業的方法的任何幫助?
uj5u.com熱心網友回復:
您res用作 express 和 axios 回呼的引數名稱的方式是這里的問題。
app.get('...', (req, res) => {
axios.get('...').then((res) => {
res.send(res.data); // here the res of axios.then is used
})
});
而是使用不同的名稱
app.get('...', (req, res) => {
axios.get('...').then((response) => {
res.send(response.data);
})
});
結帳變數范圍以獲取更多資訊
uj5u.com熱心網友回復:
要從 API 獲取資料,請嘗試如下:
const [apiResp, setApiResp] = useState([]);
useEffect(() => {
axios.get(`<api>`)
.then(res => {
const response = res.data;
setApiResp([response]);
})
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/510359.html
