ordinary-sapphireO
Refine3y ago
8 replies
ordinary-sapphire

sorting and filtering

i'm tying to sort using location and filter using name. but unsure it what i have done on the server side is working. as nothing happens when i'm using the button and text field. i get all the projects, but its not sorting or filtering
const getAllProjects = async (req, res) => {
    const {
        _end,
        _order,
        _start,
        _sort,
        name_like = "",
    } = req.query;

    const query = {};

    if (name_like) {
        query.name = { $regex: name_like, $options: "i" };
    }

    try {
        const count = await Project.countDocuments({ query });
        const projects = await Project
            .find(query)
            .limit(_end)
            .skip(_start)
            .sort({ [_sort]: _order });

        res.header("x-total-count", count);
        res.header("Access-Control-Expose-Headers", "x-total-count");

        res.status(200).json(projects);
    } catch (error) {
        res.status(500).json({ message: error.message });
    }
};
Was this page helpful?