[英]Node js mysql stored procedure call in a for loop
因此,我目前正在使用mysql npm软件包https://www.npmjs.com/package/mysql 。 我有一个要求,我必须多次调用存储过程,但要注意,后续的存储过程调用取决于先前的调用。 伪代码如下:
let mysql = require("mysql");
let mysqlPoolConnection = mysql.createPool({
connectionLimit: 20,
host: '0.0.0.0',
port: '3306',
user: 'user1',
password: 'pwd',
database: 'mysql_db'
});
for (let index = 0; index < params.length; index++) {
let sp_ProcedureCall = "CALL sp_StoredProcedure(?, ?, ?, ?)";
let sp_ProcedureParams = [params[index].firstParam, params[index].secondParam, params[index].thirdParam, params[index].fourthParam];
// This is where the issue is. I'd like to call the stored procedure once and then once I get a response from it then make subsequent calls. Basically, depending on the previous stored procedure result, I decide whether I continue with subsequent calls or not.
mysqlPoolConnection.query(sp_ProcedureCall, sp_ProcedureParams, (errorObj, responseObj, fieldsObj) => {
}
}
NodeJS是异步的,这意味着您的for循环无需等待调用结果即可进行迭代。 如果要“等待”以前的结果,则需要控制程序的流程。
看一下像async这样的库来启用这种类型的控制流。 从您的描述来看, eachSeries()可能很适合-为数组中的每个值运行一个函数,并且一次只能运行一个。 (或根据您的需要减少-也有其他选择)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.