需要查询的看这个文章https://blog.csdn.net/qq_35228658/article/details/122131124
Sequelize 增删改操作
const User = require('../model/User');
const express = require('express');
let router = express.Router();
/**
* 新增
* 使用方法 create
* 接受参数在req.body里面,对应表中的各个字段
*/
router.post('/addUser', function (req, res, next) {
User.create(req.body).then(resData => {
res.send({
code: 0,
data: resData,
msg: '保存成功'
})
}).catch(() => {
res.send({
code: 9999,
data: null,
msg: '保存失败'
})
})
})
/**
* 修改
* 使用方法 update
* 接受参数在req.body里面,对应表中的各个字段
*/
router.post('/editUser', function (req, res, next) {
const user = req.body;
User.update(user, {
where: {
id: user.id
}
}).then(resData => {
res.send({
code: 0,
data: resData,
msg: '修改成功'
})
}).catch(() => {
res.send({
code: 9999,
data: null,
msg: '修改失败'
})
})
})
/**
* 删除,例如:根据id查询
* 使用方法:destroy
* 在req.query中接受参数
*/
router.get('/userDel', async function (req, res, next) {
if(req.query.id) {
User.destroy({
where: {
id: req.query.id
}
}).then(resData => {
res.send({
code: 0,
data: resData,
msg: 'ok'
})
}).catch(() => {
res.send({
code: 9999,
data: null,
msg: '删除失败'
})
})
}else {
res.send({
code: 9999,
data: null,
msg: '参数错误'
})
}
})
module.exports = router