捕获模型错误的最佳方法? (Node.js + 续集)
Best way to catch an error in Models? (Node.js + Sequelize)
有没有一种简单的方法可以在使用 Node.js 和 Sequelize(模型)的 API(服务器)中捕获错误?这是我的代码(它使用 async + await):
const router = express.Router()
const { Operations } = require('../models')
router.post('/operations/add', async (req, res) => {
const operations = req.body
await operations.create(operations)
res.json(operations)
console.log('op added!')
})
router.put('/operations/update/:id', async (req, res) => {
const operationId = req.params.id
const operationUpdatedData = req.body
const operationById = await Operation.findOne({ where: { id: operationId } })
const operationUpdated = await operationById.update(operationUpdatedData)
res.json(operationUpdated)
console.log('op updated!')
})
router.delete('/operations/delete/:id', async (req, res) => {
const operationId = req.params.id
await Operations.destroy({ where: { id: operationId } })
res.send('op deleted!')
console.log('op deleted!')
})
module.exports = router
这是我处理客户端错误的方式:
axios.post(`http://localhost:9000/operations/add`, data)
.then(res => console.log(`op added! (${res.status})`))
.catch(err => console.log(`wrong! (${err.response.status} )`))
我不想要任何花哨的东西,但请随意尝试任何你想尝试的东西!
如果要处理特定错误,请附上 .catch
处理程序
router.post("/operations/add", async (req, res) => {
try {
const operations = req.body;
await operations.create(operations);
res.json(operations);
console.log("op added!");
} catch (error) {
// handle error here if you want to send on front simply send
console.log(error)
res.json(error.message)
}
});
如果你想更普遍地处理错误(即显示一个很好的错误消息,而不是杀死你的服务器,你可能想看看未处理的异常
https://nodejs.org/api/process.html#process_event_uncaughtexception
如果你使用的是express,它还包含一些错误处理工具http://expressjs.com/en/guide/error-handling.html
有没有一种简单的方法可以在使用 Node.js 和 Sequelize(模型)的 API(服务器)中捕获错误?这是我的代码(它使用 async + await):
const router = express.Router()
const { Operations } = require('../models')
router.post('/operations/add', async (req, res) => {
const operations = req.body
await operations.create(operations)
res.json(operations)
console.log('op added!')
})
router.put('/operations/update/:id', async (req, res) => {
const operationId = req.params.id
const operationUpdatedData = req.body
const operationById = await Operation.findOne({ where: { id: operationId } })
const operationUpdated = await operationById.update(operationUpdatedData)
res.json(operationUpdated)
console.log('op updated!')
})
router.delete('/operations/delete/:id', async (req, res) => {
const operationId = req.params.id
await Operations.destroy({ where: { id: operationId } })
res.send('op deleted!')
console.log('op deleted!')
})
module.exports = router
这是我处理客户端错误的方式:
axios.post(`http://localhost:9000/operations/add`, data)
.then(res => console.log(`op added! (${res.status})`))
.catch(err => console.log(`wrong! (${err.response.status} )`))
我不想要任何花哨的东西,但请随意尝试任何你想尝试的东西!
如果要处理特定错误,请附上 .catch
处理程序
router.post("/operations/add", async (req, res) => {
try {
const operations = req.body;
await operations.create(operations);
res.json(operations);
console.log("op added!");
} catch (error) {
// handle error here if you want to send on front simply send
console.log(error)
res.json(error.message)
}
});
如果你想更普遍地处理错误(即显示一个很好的错误消息,而不是杀死你的服务器,你可能想看看未处理的异常
https://nodejs.org/api/process.html#process_event_uncaughtexception
如果你使用的是express,它还包含一些错误处理工具http://expressjs.com/en/guide/error-handling.html