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)