user6343390
user6343390

Reputation:

ES6 Arrow Function unexpected token

I'm getting this error

[11:55:38] Unexpected token => at test.js :
175 |    // }
176 |  })
177 |  var f = (req, res, next) => {
------------------------------------^
178 |    return res.json('test');
179 |  };

When running this specific code.

var f = (req, res, next) => {
  return res.json('test');
};

app.get('/test', f);

I'm playing around with ES6 and I can't find a solution for this error even though my route was working properly and returning 'test'.

What is the problem with this snippet?

'use strict';

module.exports.controller = function (app) {

  app.get('/test', (req, res, next) => {
    return res.json('test');
  });
}

Upvotes: 1

Views: 2526

Answers (2)

user6343390
user6343390

Reputation:

I found out where it came from, the error came from the gulp-jscs. Updating gulp-jscs to 3.0.2 fixes the error.

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386578

You can write

module.exports.controller = function (app) {
    app.get('/test', (req, res, next) => res.json('test'));
}

because arrow functions return always the result of the last expression without curly brackets.

Upvotes: 0

Related Questions