首页 > 解决方案 > javascript 错误问题(--unhandled-rejections=strict)

问题描述

我正在学习 javascript,但遇到以下错误:--unhandled-rejections = strict 我要崩溃了,我受不了。这是我的代码中阻止我的部分

const router = require("express").Router();
const User = require("../models/User");
const CryptoJS = require("crypto-js");
const jwt = require("jsonwebtoken");

//REGISTER
router.post("/register", async (req, res) => {
  const newUser = new User({
  username: req.body.username,
  email: req.body.email,
  password: req.body.password,
});
try {
  const user = await newUser.save();
  req.statusCode(201).json(user);
} catch (err) {
 res.status(500).json({
      message: err,
  });
 }
});

module.exports = router;

这是我在邮递员中运行请求时收到的错误消息。我使用 post 方法,在邮递员中我没有结果我在终端中使用 npm run --unhandled-rejections = strict 但我没有相反的效果我仍然有相同的消息

 [nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
app listening on port 8800!
MongoDB connected
(node:3508) UnhandledPromiseRejectionWarning: TypeError: Cannot read 
property 'username' of undefined
at C:\Users\Philippe\Desktop\projets\reat- 
netflix\api\routes\auth.js:9:24
at Layer.handle [as handle_request] 
 (C:\Users\Philippe\Desktop\projets\reat- 
netflix\api\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Philippe\Desktop\projets\reat- 
netflix\api\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Philippe\Desktop\projets\reat- 
netflix\api\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request
(C:\Users\Philippe\Desktop\projets\reat- 
netflix\api\node_modules\express\lib\router\layer.js:95:5)
   at C:\Users\Philippe\Desktop\projets\reat- 
netflix\api\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Philippe\Desktop\projets\reat- 
netflix\api\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Philippe\Desktop\projets\reat- 
netflix\api\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\Users\Philippe\Desktop\projets\reat- 
netflix\api\node_modules\express\lib\router\index.js:174:3)
 at router (C:\Users\Philippe\Desktop\projets\reat- 
netflix\api\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] 


(C:\Users\Philippe\Desktop\projets\reat- 
netflix\api\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Philippe\Desktop\projets\reat- 
netflix\api\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Philippe\Desktop\projets\reat- 
netflix\api\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Philippe\Desktop\projets\reat- 
netflix\api\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Philippe\Desktop\projets\reat- 
netflix\api\node_modules\express\lib\router\index.js:275:10)
at expressInit (C:\Users\Philippe\Desktop\projets\reat- 
netflix\api\node_modules\express\lib\middleware\init.js:40:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3508) UnhandledPromiseRejectionWarning: Unhandled promise 
rejection. This error originated either by throwing inside of an async 
function without a catch block, or by rejecting a promise which was not 
 handled with .catch(). To terminate the node process on unhandled 
 promise rejection, use the CLI flag `--unhandled-rejections=strict` 
 (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). 
 (rejection id: 1)
 (node:3508) [DEP0018] DeprecationWarning: Unhandled promise rejections 
 are deprecated. In the future, promise rejections that are not handled  
will 
terminate the Node.js process with a non-zero exit code.

标签: javascript

解决方案


为了防止未处理的拒绝,即处理错误,您还需要将请求处理程序的第一条语句放入try块中:

router.post("/register", async (req, res) => {
  try {
    const newUser = new User({
      username: req.body.username,
      email: req.body.email,
      password: req.body.password,
    });
    const user = await newUser.save();
    req.statusCode(201).json(user);
  } catch (err) {
    res.status(500).json({
      message: err,
    });
  }
});

为了防止错误本身,您可能需要修复填充req.body.


推荐阅读