【发布时间】:2020-06-13 22:06:09
【问题描述】:
这个节点休息 api 来获取和创建记录,在我们将最后一个 json 字段放在首位时进行创建。
router.post('/audit', async (req,res)=> {
const _audit=req.body;
try {
const __audit= await audit.create(_audit)
res.status(201).json(__audit)
} catch (err) {
res.status(400).send(err)
}
});
JSON 在解释为什么 updatedDate 排在第一位
{
"updatedDate": "2020-03-01T11:35:27.984Z",
"id": 11,
"updatedBy": "Rod",
"actions": "added",
"oldData": "updated",
"newData": "change",
"entityType": "admin1",
"entityId": "53"
}
router.get('/audit',(req,res)=>{
audit.findAll()
.then(a=>{
res.json(a)
}).catch(err=>{
res.send('error'+err)
res.status(400)
})
});
JSON 这里返回的 json 是完美的,同时通过 findall() 获取所有数据,同时它也适用于另一个 http 请求。
[
{
"id": 4,
"updatedBy": "Rod",
"actions": "added",
"oldData": "updated",
"newData": "change",
"entityType": "admin1",
"entityId": "53",
"updatedDate": "2020-03-01T11:07:46.000Z"
}
]
模型这是用mysql sequelize提取的模型
const Sequelize=require('sequelize')
const db= require('../database/db')
module.exports = db.sequelize.define(
'audit',
{
id: {
type: Sequelize.INTEGER(11),
autoIncrement:true,
primaryKey: true
},
updatedBy: {
type:Sequelize.STRING(45),
field: 'updated_by'
},
actions: {
type:Sequelize.STRING(45),
field:'actions'
},
oldData: {
type:Sequelize.STRING(45),
field:'old_data'
},
newData: {
type:Sequelize.STRING(45),
field:'new_data'
},
entityType: {
type:Sequelize.STRING(45),
field:'entity_type'
},
entityId: {
type:Sequelize.STRING(45),
field:'entity_id'
},
updatedDate: {
type:Sequelize.DATE,
field:'updated_date',
defaultValue: Sequelize.NOW
}
},
{
freezeTableName: true
}
)
【问题讨论】:
标签: node.js express sequelize.js