【问题标题】:Response is undefined in post request (Express)发布请求中未定义响应(快递)
【发布时间】:2017-10-24 10:56:18
【问题描述】:

当我转到指定的路由时,我有一个路由处理程序,它调用身份验证控制器中的注册函数:

module.exports = app => {
  app.post("/signup", authentication.signup());
};

我的身份验证控制器:

exports.signup = function(req, res, next) {
  res.send({ success: true });
};

但是当我启动服务器时,我说它无法读取未定义的属性“发送”。由于甚至无法运行我的服务器,我无法使用邮递员来测试我的 API 路由。

为什么它在我访问路由之前就尝试调用发送?我正在使用 node http 库和我的 express 应用程序运行我的服务器。

【问题讨论】:

  • app.post("/signup", authentication.signup);

标签: javascript node.js express routes response


【解决方案1】:

app.post("/signup", authentication.signup()); 调用 authentication.signup 并将其返回值传递给app.post,与foo(bar()) 调用 bar 并将其返回值传递给foo.

相反,只需将函数本身传递给app.post; Express 稍后将调用它以响应该路由上的 POST:

module.exports = app => {
  app.post("/signup", authentication.signup);
  // No () here ---------------------------^
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多