node搭建个人博客promise警告解除
2024-10-07 07:07:47
警告
(node:8500) UnhandledPromiseRejectionWarning: undefined
(node:8500) 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(). (rejection id: 2)
(node:8500) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with
报错代码
//数据库中是否已经存在同名分类名称
Category.findOne({
name: name
}).then(function (rs) {
if (rs) {
//数据库中已经存在该分类
res.render('admin/error',{
userInfo: req.userInfo,
message: '分类已经存在'
});
return Promise.reject();
} else {
//数据库中不存在该分类,可以保存
return new Category({
name: name
}).save();
}
}).then(function (newCategory) {
res.render('admin/success',{
userInfo: req.userInfo,
message: '分类保存成功',
url: '/admin/category'
});
})
报错原因:因为Promise的reject没有被处理。
如果不管异常内容,直接丢弃异常,可以这样处理:.catch(()=>{});
修改后代码
//数据库中是否已经存在同名分类名称
Category.findOne({
name: name
}).then(function (rs) {
if (rs) {
//数据库中已经存在该分类
res.render('admin/error',{
userInfo: req.userInfo,
message: '分类已经存在'
});
return Promise.reject();
} else {
//数据库中不存在该分类,可以保存
return new Category({
name: name
}).save();
}
}).then(function (newCategory) {
res.render('admin/success',{
userInfo: req.userInfo,
message: '分类保存成功',
url: '/admin/category'
});
}).catch(()=>{});
最新文章
- Linux sort 命令
- web-inf目录和meta-inf目录
- 【javascript】箭头函数
- null
- The Nine Indispensable Rules for HW/SW Debugging 软硬件调试之9条军规
- rabbitmq redis
- C语言输出规定长度的整数,不够位数前面补零
- Winform 控件使用集锦
- css布局小技巧 2016.03.06
- Vxworks、QNX、Xenomai、Intime、Sylixos、Ucos等实时操作系统的性能特点
- c/c++重定向输入输出
- python开发 getpass获取操作系统登陆名
- git初试
- WIP完工入库及完工退回的几个重要问题
- Windows系统pip安装whl包
- Python高级笔记(二) -- 深拷贝和浅拷贝
- selenium——find_element_by_xx 与 find_element(By.XX,'XXXX')
- 1分钟快速制作漂亮的H5本地记事本
- 如何去掉C#字符串中的所有空格
- 铁板纹理 铁锈Rust