express-mongoose-es6-rest-api icon indicating copy to clipboard operation
express-mongoose-es6-rest-api copied to clipboard

support async/await function

Open lednhatkhanh opened this issue 8 years ago • 3 comments

Hello, when I try to change my function using async/await like this:

const login = async (req, res, next) => {
  const user = await User.findOne({ email: req.body.email });

  if (!user || !bcrypt.compareSync(req.body.password, user.password)) {
    const err = new APIError('Authentication error', httpStatus.UNAUTHORIZED, true);
    return next(err);
  }

  const token = jwt.sign({
    email: user.email
  }, config.jwtSecret);
  return res.json({
    token
  });
};

I got this error when running yarn start

E:\Coding\beautypediavn\beautypediavn-backend-v2\dist\server\controllers\auth.controller.js:79
  var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(req, res, next) {
                               ^

ReferenceError: regeneratorRuntime is not defined
    at E:\Coding\beautypediavn\beautypediavn-backend-v2\dist\server\controllers\auth.controller.js:79:32
    at Object.<anonymous> (E:\Coding\beautypediavn\beautypediavn-backend-v2\dist\server\controllers\auth.controller.js:118:2)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (E:\Coding\beautypediavn\beautypediavn-backend-v2\dist\server\routes\auth.route.js:23:13)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (E:\Coding\beautypediavn\beautypediavn-backend-v2\dist\server\routes\index.route.js:15:13)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)

Any ideas on how to fix this or supporting this? Thank you, I'm not so used to these config things.

lednhatkhanh avatar May 30 '17 15:05 lednhatkhanh

I can confirm @fcpauldiaz solution!

osahner avatar May 30 '17 17:05 osahner

@osahner thank you but it doesn't work

lednhatkhanh avatar May 31 '17 02:05 lednhatkhanh

@lednhatkhanh install the babel-polyfill and import it before all other modules in index.js

import polyfill from 'babel-polyfill'; // eslint-disable-line

fcpauldiaz avatar Jul 03 '17 17:07 fcpauldiaz