到目前为止,我正在用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
我感谢任何指导。干杯。
答案 0 :(得分: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