先看报错:
(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 /api/#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 process with a non-zero exit code.
代码:
import db from '../db/'
//导出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
})
/*(200).json({
status: 0,
message: 'success',
data: rows
})*/
}
// (getAllUser())
getAllUser()
/*.catch(() => {
('error already done')
})*/
主要报错是:UnhandledPromiseRejectionWarning
,关于这个也不再赘述,毕竟有很多博客写过这个报错的原因,
它报错在send,说明getAllUser()
这个方法没有失败的处理函数,需要在调用时,处理:
getAllUser()
.then(
()=>{
console.log('then')
},()=>{
console.log('catch')
}
)
// 或者:
getAllUser()
.catch(() => {
console.log('error already done')
})