最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

错误:找不到apiv1userssignup,但Postman显示了路线数据

网站源码admin20浏览0评论

错误:找不到/api/v1/users/signup,但Postman显示了路线数据

错误:找不到/api/v1/users/signup,但Postman显示了路线数据

我在 /signup 和 /login 路线中遇到奇怪的错误

Vscode 不断向我显示“错误:在服务器上找不到 /api/v1/users/signup :(!”

另一方面,Postman 的状态为“成功”,一切运行正常且符合预期。

/login 也会发生同样的事情

以下是相关代码:

app.js:

app.use("/api/v1/users", userRouter);

usersRoute.js:

router.post("/signup", authController.signup);
router.post("/login", authController.login);

注册和登录的标题:

exports.signup = catchAsync(async (request, response, next) => {}
exports.login = catchAsync(async (request, response, next) => {}

我收到错误的代码片段在这里: 注意:我添加了 console.log 来确定错误发生的位置,我发现它是在从 Postman 发出 POST 请求后发生的,在我的控制台中打印的第一件事是

console.log("INSIDE CATCHASYNC 2");

const catchAsync = (asyncFunction) => {  
  console.log("INSIDE CATCHASYNC 1");
  
  return (request, response, next) => {
    console.log("INSIDE CATCHASYNC 2");
    asyncFunction(request, response, next).catch((error) => {
      console.log(error);
      console.log(error.stack);
      
      console.log("INSIDE CATCHASYNC 3");
      next(error);
    });
  };
};

exports.login = catchAsync(async (request, response, next) => {
  console.log("++++++1+++++++");
  
  const { email, password } = request.body;

  // 1- checking email and pawword :
  if (!email || !password)
    return next(new AppError(400, "Please provide your email and password to login !"));

  // 2- checking if the user is exists and the entered data match with db data or not :
  const user = await User.findOne({ email }) // we're using findOne not ById because in the situation we're not selecting a user by its id but with user's email .
    .select("+password"); 

  // 3- okay? send the token to the user:
  if (!user || !(await userparePassword(password, user.password)))
    return next(new AppError(401, "Incorrect email or password !"));
  
    console.log("++++++2+++++++");
  const token = signature(user._id);
  console.log("++++++12+++++++");
  response.status(200).json({
    status: "success",
    token
  });
  console.log("above next() in login");

  next();
});

这是控制台的屏幕截图:

回答如下:

在这段代码中

exports.login = catchAsync(async (request, response, next) => {
  console.log("++++++1+++++++");
  
  const { email, password } = request.body;

  // 1- checking email and pawword :
  if (!email || !password)
    return next(new AppError(400, "Please provide your email and password to login !"));

  // 2- checking if the user is exists and the entered data match with db data or not :
  const user = await User.findOne({ email }) // we're using findOne not ById because in the situation we're not selecting a user by its id but with user's email .
    .select("+password"); 

  // 3- okay? send the token to the user:
  if (!user || !(await userparePassword(password, user.password)))
    return next(new AppError(401, "Incorrect email or password !"));
  
    console.log("++++++2+++++++");
  const token = signature(user._id);
  console.log("++++++12+++++++");
  response.status(200).json({
    status: "success",
    token
  });
  console.log("above next() in login");

  next();
});

您不应该在最后

next()
执行调用,因为 next 应该用于传递到下一个中间件,但正如我所看到的,您正在执行响应返回

response.status(200).json({
    status: "success",
    token
  });

所以你不希望有下一个中间件,无论如何,即使你想执行另一个中间件,你也应该将

request
response
对象传递给它,就像这样
next(request,response,next)
以确保下一个中间件可以访问之前的请求状态。

如果我理解不好,请更新您的问题,我会改进我的答案

发布评论

评论列表(0)

  1. 暂无评论