我一直尝试使用 Axios 发送 API 请求,看起来从数据参数读取用户 id 时出现问题,我正在从本地存储读取用户 id
const getAuthUser=(data)=>{
if(localStorage.getItem("user")){
return JSON.parse(localStorage.getItem("user"))
}
}
const localStorage = require("../helper/Local_storage")
const auth= localStorage.getAuthUser()
useEffect(()=>{
let userId = JSON.stringify({
"user_id": auth.id
});
let config = {
method: 'get',
maxBodyLength: Infinity,
url: 'http://localhost:4000/borrowing_request',
headers: {
'token': auth.token,
},
data: userId
};
axios.request(config)
.then((res) => {
setBorrowedBooks({...borrowedBooks, results:res.data, loading:false})
})
.catch((err) => {
console.log(err);
});
},[])