【发布时间】:2018-12-18 20:20:20
【问题描述】:
我正在使用 nodejs、sequelize、dan MySQL 数据库 (10.1.21-MariaDB) 构建一个 API。当我尝试做一些 PATCHing(更新数据)时,它会抛出 AssertionErrors,但它适用于 POST(插入数据)。
这是我的补丁代码:
const express = require('express');
const router = express.Router();
const brandModel = sequelize.define('tbl_brand', {
brand_name: {
type: Sequelize.STRING,
allowNull: false
},
}, {
freezeTableName: true,
});
router.patch('/:id', (req, res, next) => {
const id = req.params.id;
const newModel = {
brand_name: req.body.brand_name
};
sequelize.authenticate().then(() => {
const promise = brandModel.update(newModel, {brand_id: id});
return promise.then(function(item){
res.status(201).json({
success: true,
result: item
});
});
})
.catch(err => {
res.status(500).json({
success: false,
result: err
});
});
});
我使用邮递员并像这样访问它:
http://localhost:3000/brand/1
使用原始 JSON:
{
"brand_name" : "Adidasssss"
}
结果如下:
{
"success": false,
"result": {
"generatedMessage": false,
"name": "AssertionError [ERR_ASSERTION]",
"code": "ERR_ASSERTION",
"expected": true,
"operator": "=="
}
}
可能是什么问题?
【问题讨论】:
标签: mysql node.js sequelize.js