Monday, 8 July 2019

GET data and filter, or GET filtered data using ajax and nodejs

I'm developing a web app using a Node.js/express backend and MongoDB as a database.

I am building up a page where I will make an interactive table using DataTables for different types of user ( currently have two: mentors and mentees). This example requires just two data requests (one for each user type), but my final page will be more like 10.

For each user type, I am making an ajax get call for each user type, and building the table from the returned data:

Mentees

$.get('/admin/' + id + '/mentees')
    .done(data => {
        $('#menteeTable').DataTable( {
            data: data,
            "columns": [
                { "data": "username"},
                { "data": "status"}
            ]
        });
    })

Mentors

$.get('/admin/' + id + '/mentors')
    .done(data => {
        $('#mentorTable').DataTable( {
            data: data,
            "columns": [
                { "data": "username"},
                { "data": "position"}
            ]
        });
    })

This then requires two routes in my Node.js backend:

router.get("/admin/:id/mentors", getMentors);
router.get("/admin/:id/mentees", getMentees);

And two controllers, that are structured identically (but filter for differnt user types):

getMentees(req, res, next){
    console.log("Controller: getMentees");
    let query = { accountType: 'mentee', isAdmin: false };
    Profile.find(query)
        .lean()
        .then(users => {
            return res.json(users);
        })
        .catch(err => {
            console.log(err)
        })
}

This works great. However, as I want to make multiple data requests I want to make sure that I'm building this the right way and can see several options:

  1. Make individual ajax calls for each data type, and do any heavy lifting on the backend (e.g. tally user types and return) - as above
  2. Make individual ajax calls for each data type, but do the heavy lifting on the frontend. In the above example I could have just as easily filtered out isAdmin users on the data returned from my ajax call
  3. Make fewer ajax calls that request less refined data. In the above example I could have made one call (requiring only one route/controller) for all users, and then filtered data on the frontend to build two tables

I would love some advice on which strategy is most efficient in terms of time spent sourcing data


UPDATE

To clarify the question, I could have achieved the same result as above using a controller setup something like this:

Profile.find(query)
    .lean()
    .then(users => {
        let mentors = [],
        mentees = []

        users.forEach(user => {

            if(user.accountType === 'mentee') {
                mentees.push(user);
            } else if (user.accountType === 'mentor') {
                mentors.push(user);
            }
        });
        return res.json({mentees, mentors});
    })

And then make one ajax call, and split the data accordingly. My question is: which is the preferred option?



from GET data and filter, or GET filtered data using ajax and nodejs

No comments:

Post a Comment