使用 mysqljs 通过 express.js 中 webAPI 端点的存储过程查询 mySQL 数据库。我需要返回插入的对象。为此,我尝试根据 mysqljs 的文档访问 insrtedId。但insertedid 总是返回零。
我试图在存储过程中包含输出参数并将其设置为 LAST_INSERT_ID()。仍然insertedId为0
router.post("/", (req, res) => {
name = req.body.name;
apiconnection.query(
`CALL userAdd ('${name}', @_LID)`,
(error, rows, fields) => {
if (error) {
res.json({ message: `cant be saved to the database` });
} else {
const id = rows.insertId;
router.get("/", (req, res) => {
apiconnection.query(
`select * from tbl1 where id = ${id}`,
(error, rows, fields) => {
if (!error) {
res.json(rows);
} else {
res.json(error);
}
}
);
});
}
}
);
});
here is the stored procedure
```CREATE DEFINER=`root`@`localhost` PROCEDURE `userAdd`(IN _name varchar(250), OUT _LID int)
BEGIN
insert into tbl1(name) values (_name);
set _LID = LAST_INSERT_ID();
END```
note that the id is set to auto increment