I am currently trying to send a 404 page that is a .pug file whenever a an ID params is not in the database. I am calling next() in the api route, and when I do it inside the elastics search callback, it will throw a page not found error as well as two Cant set header after they are sent messages. When I call the next() outside, it will properly just show the 404 error message and not the header message. I am unsure of why it is doing this.
//Doing it this way will properly send the 404 page.
app.get('/redirect/:id', function(req, res, next) {
        let found = false;
        if (!found) {
           return next();
        }
});
//But i need to check if an ID exists and then throw the 404, but doing it this way will not work and I will keep getting an error about not being able to set headers and the 404 message.
app.get('/redirect/:id', function(req, res, next) {
    search.byId(req.params.id, (err, card) => {
        if (err) log.log(err);
        if (!card || card === undefined) {
            return next();
        }
    });
});