I'm studying NodeJS with expressjs and so far, so good, but I have this little issue: calling a stored procedure from Workbench everything runs OK, but same sentence (copy/paste) within express shows error from MySQL engine.
{
"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
}
Here's my NodeJS call:
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 from Workbench
CALL `pinturas`.`add_inventory`('101', '3');
and finally, stored procedure:
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
I appreciate any guidance. Cheers.
So guys, after a few checks and attempts, I managed to solve my problem, that actually was pretty simple and all entirely in my Stored Procedure script. I was calling parameters with @ but actually those are for variables declared within the code, not input parameters.
Here's my updated code.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.