Normally when using Promise syntax, the below syntaxes produce the same result:
// Syntax A - works fine
getUser(id).then((user) => console.log(user)
// Syntax B - works fine
getUser(id).then(console.log)
Yet when we attempt this in an Express route, only syntax A works:
// Works
app.get('/syntax_a', (req, res) => {
getUser(req.body.id).then((user) => res.json(user))
})
// Fails
app.get('/syntax_b', (req, res) => {
getUser(req.body.id).then(res.json)
})
Syntax B yields an error:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'app' of undefined
at json (/server/node_modules/express/lib/response.js:256:18)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
Why isn't this working like the first example?