0

我正在用 expressjs 研究 NodeJS,到目前为止,一切都很好,但我有一个小问题:从 Workbench 调用存储过程一切运行正常,但是 express 中的同一句话(复制/粘贴)显示来自 MySQL 引擎的错误。

{
    "desc_status": {
        "code": "ER_BAD_NULL_ERROR",
        "errno": 1048,
        "sqlMessage": "Column 'id_color' cannot be null",
        "sqlState": "23000",
        "index": 0,
        "sql": "CALL pinturas.add_inventory('101', '3');"
    },
    "code_status": 409
}

这是我的 NodeJS 调用:

let sql = "CALL pinturas.add_inventory(101, 3);";
conn.query(sql, true, (err, filas) => {
    if(err){
        //next(err);
        res.status(409).json({
            desc_status: err,
            code_status: 409,
        });
        return;
    }
    (...)
});

从工作台调用

CALL `pinturas`.`add_inventory`('101', '3');

最后,存储过程:

CREATE DEFINER=`root`@`localhost` PROCEDURE `add_inventory`(
    IN _id_color INT(11), 
    IN _cant INT(11)
)
BEGIN
    set @numRows = (select count(*) from stock_colores where id_pintura = 1 and id_color = @_id_color);
    if @numRows > 0 then
        set @oldCant = (select cant from stock_colores where id_pintura = 1 and id_color = @_id_color);
        set @_stock_id = (select stock_id from stock_colores where id_pintura = 1 and id_color = @_id_color);
        update stock_colores set cant = (_cant + @oldCant) where stock_id = @_stock_id;
    else
        insert into stock_colores (id_pintura, id_color, cant) values (1, @_id_color, _cant);
    end if;
END

我很感激任何指导。干杯。

4

1 回答 1

0

所以伙计们,经过几次检查和尝试,我设法解决了我的问题,这实际上非常简单,完全在我的存储过程脚本中。我用 @ 调用参数,但实际上这些是在代码中声明的变量,而不是输入参数。

这是我更新的代码。

CREATE DEFINER=`root`@`localhost` PROCEDURE `add_inventory`(
    IN _id_pintura INT(2), 
    IN _id_color INT(2), 
    IN _cant INT(2)
)
BEGIN
    set @numRows = (select count(*) from stock_colores where id_pintura = _id_pintura and id_color = _id_color);
    if @numRows > 0 then
        set @oldCant = (select cant from stock_colores where id_pintura = _id_pintura and id_color = _id_color);
        set @_stock_id = (select stock_id from stock_colores where id_pintura = _id_pintura and id_color = _id_color);
        update stock_colores set cant = (_cant + @oldCant) where stock_id = @_stock_id;
    else
        insert into stock_colores (id_pintura, id_color, cant) values (_id_pintura, _id_color, _cant);
    end if;
END
于 2018-07-04T15:07:39.800 回答