javascript - 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.
解决方案
为了防止未处理的拒绝,即处理错误,您还需要将请求处理程序的第一条语句放入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
.
推荐阅读
- laravel - 如何在 laravel 中将商品添加到购物车?
- r - 使用 ggplot 使用列名绘制 dplyr 汇总 tibble
- html - 如果我使用或条件禁用输入复选框,则不会将其模型值传递给控制器
- apache-kafka - 卡夫卡提交差距
- ios - 如何在框架中使用 @objc 扩展?
- cmd - 使用 cmd 从子文件夹中删除特定文件
- java - 没有在android studio上打开谷歌地图就无法获取位置
- python-3.x - 在 pycharm 中为每次运行显示来自 mlflow 的度量值
- python - 如何使用 xlrd 将 excel 转换为嵌套的 JSON?
- javascript - JSON Parse 错误:带有 localStorage 的意外标识符“未定义”