React - Axios 请求页面和限制不起作用
React - Axios request page and limit not working
为什么这仍在加载所有数据,而不仅仅是前 15 个数据?
const [page, setPage] = useState(1);
useEffect(() => {
const getHistory = async () => {
const res = await axios.get(`/api/payment?page=${page}&limit=`, {
headers: { Authorization: token },
});
setHistory(res.data);
};
getHistory();
}, []);`
这是后端发生的事情
getPayments: async (req, res) => {
try {
const payments = await Payments.find();
res.json(payments);
} catch (err) {
return res.status(500).json({ msg: err.message });
}
}
您实际上应该在 getPayments
函数中检索和使用您的查询参数:
getPayments: async (req, res) => {
try {
const { page, limit } = req.query
const payments = await Payments.find().skip(page * limit).limit(limit).exec();
res.json(payments);
} catch (err) {
return res.status(500).json({ msg: err.message });
}
};
为什么这仍在加载所有数据,而不仅仅是前 15 个数据?
const [page, setPage] = useState(1);
useEffect(() => {
const getHistory = async () => {
const res = await axios.get(`/api/payment?page=${page}&limit=`, {
headers: { Authorization: token },
});
setHistory(res.data);
};
getHistory();
}, []);`
这是后端发生的事情
getPayments: async (req, res) => {
try {
const payments = await Payments.find();
res.json(payments);
} catch (err) {
return res.status(500).json({ msg: err.message });
}
}
您实际上应该在 getPayments
函数中检索和使用您的查询参数:
getPayments: async (req, res) => {
try {
const { page, limit } = req.query
const payments = await Payments.find().skip(page * limit).limit(limit).exec();
res.json(payments);
} catch (err) {
return res.status(500).json({ msg: err.message });
}
};