UnhandledPromiseRejectionWarning报错send()的处理

43 篇文章 1 订阅
20 篇文章 2 订阅

先看报错:

(node:24460) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined
    at getAllUser (file:///G:/MarkDownNote/vue/example_code/apiTest/controler/user_ctrl.js:6:6)   
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:24460) 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(). T
o 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:24460) [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.

代码:

import db from '../db/index.js'

//导出getAllUser
export async function getAllUser (req, res) {
	const [rows] = await db.query('select id,username,nickname from ev_user')
	res.send({
		status: 0,
		message: 'success',
		data: rows
	})
	
	/*res.status(200).json({
		status: 0,
		message: 'success',
		data: rows
	})*/
}

// console.log(getAllUser())

getAllUser()
	/*.catch(() => {
	console.log('error already done')
})*/

主要报错是:UnhandledPromiseRejectionWarning,关于这个也不再赘述,毕竟有很多博客写过这个报错的原因,
它报错在send,说明getAllUser()这个方法没有失败的处理函数,需要在调用时,处理:

getAllUser()
	.then(
		()=>{
			console.log('then')
		},()=>{
			console.log('catch')
		}
	)
// 或者:
getAllUser()
	.catch(() => {
	console.log('error already done')
})
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无名之辈无名之辈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值