在此处输入图像描述
我需要从这个api端点获取数据,并将其发送到我的客户端(React(。前端和后端之间的一切都很好,但我似乎不知道如何在/dailyscores端点中获取数据并使用axios发送数据。有什么帮助吗?为什么res.send不是内部的函数。那么,以及让它工作的方法?
使用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);
})
});
查看更多信息的变量范围
要从API获取数据,请按以下方式尝试:
const [apiResp, setApiResp] = useState([]);
useEffect(() => {
axios.get(`<api>`)
.then(res => {
const response = res.data;
setApiResp([response]);
})
});