1

和之间有什么不同module.exports = testMethod ;module.exports = { testMethod } ;因为当我使用module.exports = testMethod ;它时会抛出如下错误。 错误:Route.get() 需要一个回调函数,但得到一个 [object Undefined] 但我可以接受module.exports = { testMethod } ;

整个代码是

const testMethod = asyncErrorWrapper(async (req, res, next) => {
  const information = req.body;

  const question = await Question.create({
    title: information.title,
    content: information.content,
    user: req.user.id,
  });

  res.status(200).json({
    success: true,
    data: question,
  });
});

module.exports = { testMethod };
4

2 回答 2

0

从 VSCode 中,从 ES5 或 ES6 版本到 Js 之间的更改可能会让您走上一条糟糕的道路。所以,小心点,我最近也有同样的问题,在 ES6 上使用重构module.exports = router到一些 Js 文件的末尾(使用 express 的 Node 项目)之后它就完成了。

对我来说很奇怪,在 AWS 上的 Cloud9 上我没有问题。

于 2021-01-07T06:07:00.640 回答
0

两者都用于将您的模块导出到外部功能。但是当您使用任何回调函数时

module.exports = somectrl

那么它会失败但是

module.exports = { somectrl }

因为当您创建一个对象时,它实际上会实例化它,但是当您传递一个 ref 函数/ const 函数名称时,它将表现为一个无法正常工作的现有函数。

你可以做这样的事情来工作,

module.exports = somectrl()

或者

module.exports = new somectrl()
于 2021-01-07T08:19:53.283 回答