The problem pointed out by this in js

  node.js, question

This writing can be called normally.

router.route('/account')
 .get(function (req, res, next) {
 User.find()
 .then(users => res.json(users))
 .catch(err => next(err));
 });

I think this code can be simplified to

router.route('/account')
 .get(function (req, res, next) {
 User.find()
 .then(res.json)
 .catch(next);
 });

However, when using the second writing method, an error will be reported in the source code of express.

res.json = function json(obj) {
 var val = obj;
 
 // allow status / body
 if (arguments.length === 2) {
 // res.json(body, status) backwards compat
 if (typeof arguments[1] === 'number') {
 deprecate('res.json(obj, status): Use res.status(status).json(obj) instead');
 this.statusCode = arguments[1];
 } else {
 deprecate('res.json(status, obj): Use res.status(status).json(obj) instead');
 this.statusCode = arguments[0];
 val = arguments[1];
 bracket
 bracket
 
 // settings
 var app = this.app;
 var replacer = app.get('json replacer');
 var spaces = app.get('json spaces');
 var body = JSON.stringify(val, replacer, spaces);
 
 // content-type
 if (!  this.get('Content-Type')) {
 this.set('Content-Type', 'application/json');
 bracket
 
 return this.send(body);
 };

It is in

var app = this.app;

In this line, debug found that this value is undefined. In js, isn’t this the method of the object that should point directly to the object? Why is this here, that is, res, undefined?

These two sentences are not equivalent. . . .

.then(users => res.json(users))
 .then(res.json)

The implementation of the then function is probably as follows

function then(fn)
 {   //  ....
 fn(args);
 bracket

So in fact, the two writing methods are:

var fn = function(users)
 bracket
 res.json(users)
 bracket
 fn(users);
 // 。  .  .  .  .
 fn = res.json;
 fn(users)

These two call methods, injsonThe value of this function inside is different.
See my blog for details
http://zonxin.github.io/post/ …