I have the following route in my Express app that sets a cookie after a session has been written to the database. Then, after the session has been written the database, the route performs res.render which simply just contains a redirect in the header to take the user to /login. The problem I'm having though is that when the below function is ran I get the infamous "cant set headers after they have been sent" error and I'm a little confused as to why this is happening?
For cookies, I'm using cookies.
Thanks in advance!
.get(function (req, res) {
        userTable.find({_id: req.query.id}, function (err, obj) {
            var newSession = new sessionsTable({
                for_user: obj[0]._id
            });
            var cookies = new Cookies(req, res);
            newSession.save(function (err, session) {
                if (err)
                    console.log(err)
                cookies 
                    .set('_sessionStarted', true, { httpOnly: false, overwrite: false });
            });
        });
        res.render('activate-session.ejs');
    });