0

In my project I want to overwrite the res.render method to always add an option, no matter what other options there are. When I try to do it I get the error mentioned above. Here's my current middleware code:

app.use((_, res, next) => {
    const oldRender = res.render;

    res.render = (view, options, callback) => {
        if (typeof options === "function") {
            callback = options;
            options = {};
        }

        options.stops = stops?.data || {};

        oldRender.call(this, view, options, callback);
    };

    next();
});
ProGamer2711
  • 451
  • 1
  • 5
  • 18
  • If you're trying to just make sure there's always a particular property available to your template, why not just assign it to `res.locals.stops = ...`. That's what `res.locals` is for. – jfriend00 Apr 23 '22 at 17:59
  • I suspect that your code isn't working because `this` is the wrong value. You're using arrow functions so `this` will be the lexical `this`, not the `this` that was originally passed to your method (e.g. not `res` which is what you need it to be). But, as I said in my previous comment, I think this is the hard way to solve the original problem. – jfriend00 Apr 23 '22 at 18:00
  • @jfriend00 the `this` thing fixed it. I changed the arrow function to a function. As for `res.locals` could you link an article so I can read about it? – ProGamer2711 Apr 23 '22 at 18:08
  • Reference link for `res.locals` is in my answer below. – jfriend00 Apr 23 '22 at 18:11

1 Answers1

0

It looks to me like the point of this code is to always pass a particular value to your templates without requiring every caller of res.render() to add that to the data they pass to the template.

The general-purpose way to solve this type of problem is to put a property in res.locals. The template system looks there in addition to the data passed to res.render() and this is the easy way to pass data to all your templates:

app.use((_, res, next) => {
    res.locals.stops = stops?.data || {};
    next();
});

If there's some reason that won't work for you, then the apparent problem to the solution you're trying is that you're using arrow function so the value of this you're using in oldRender.call(this, view, options, callback); is the lexical value of this, not res which is what you need it to be. You can change this:

oldRender.call(this, view, options, callback);

to this:

return oldRender.call(res, view, options, callback);

You can directly pass res instead of trying to use this and you should also return the return value (which will be res) to duplicate the original behavior.


Doc link on res.locals.

A few other answers about res.locals:

Difference between assigning to res and res.locals in node.js (Express)

Is this bad practice for res.locals? (Node.js, express)

jfriend00
  • 683,504
  • 96
  • 985
  • 979