本文主要是介绍NodeJS 过滤,分页和排序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
// GET /tasks?completed=true
// GET /tasks?limit=2&skip=2
// GET /tasks?sortBy=createdAt:desc
router.get("/tasks", auth, async (req, res) => {
const match = {};
const sort = {};
if (req.query.completed) {
match.completed = req.query.completed === "true";
}
if (req.query.sortBy) {
const parts = req.query.sortBy.split(":");
sort[parts[0]] = parts[1] === 'desc' ? -1 : 1;
}
try {
await req.user.populate({
path: "tasks",
match,
options: {
limit: parseInt(req.query.limit || 0),
skip: parseInt(req.query.skip || 0),
sort
},
});
res.send(req.user.tasks);
} catch (e) {
res.send(e);
}
});
这篇关于NodeJS 过滤,分页和排序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!