【问题标题】:Sequelize throw AssertionErrors with MySQL用 MySQL 续集抛出 AssertionErrors
【发布时间】: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


    【解决方案1】:

    没关系...我很粗心,我正在更新一个名为brandModel 的空实例。应该先搜索再更新

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 2020-10-03
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      相关资源
      最近更新 更多