Mysql 串行执行nodejs
Mysql serial execution nodejs
mysqldb.js
const mysqlDatabasePool = mysql.createPool({
connectionLimit: 50,
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USERNAME,
password: process.env.MYSQL_PASSWORD,
port: process.env.MYSQL_PORT,
database: process.env.MYSQL_DATABASE
});
mysqlDatabasePool.getConnection({
//Basic sanity checking code here
});
mysqlDatabasePool.query = util.promisify(mysqlDatabasePool.query);
module.exports = mysqlDatabasePool;
employeeController.js
var pool = require("mysqldb");
pool.query("SELECT CategoryID, CategoryIcon FROM Categories WHERE CategoryName = ?", [categoryName], function(error, results, fields) {
if(results !== null && results.length > 0) {
for(var i = 0; i < results.length; i++) {
outputData[i] = results[i];
var subCategories = pool.query("SELECT SubcategoryID FROM Subcategories WHERE CategoryID = ?", [results[i].CategoryID], function(error, results, fields) {
return results;
});
if(subCategories.length > 0)
outputData[i]["Subcategories"] = subCategories;
else
outputData[i]["Subcategories"] = null;
}
}
});
outputData[i]["Subcategories"]
的内容总是 null
而它应该有子类别的集合,因为它总是可用的。我尝试通过将 await 放在第二个 pool.query
之前并使第一个 pool.query
闭包函数异步来实现 await/async
。
应该如何处理?
作为参数传递给 pool.query
的函数是回调。这意味着您无法知道它何时会被执行。
您可以使用 async/await 来阻止调用,直到 pool.query 给您一个结果。
您可能需要在 pool.query
上使用 util.promisify
以使其 return 成为 Promise。
try {
const results = await pool.query("SELECT CategoryID, CategoryIcon FROM Categories WHERE CategoryName = ?", [categoryName]);
if(results !== null && results.length > 0) {
for(var i = 0; i < results.length; i++) {
outputData[i] = results[i];
const subCategories = await pool.query("SELECT SubcategoryID FROM Subcategories WHERE CategoryID = ?", [results[i].CategoryID]);
if(subCategories.length > 0)
outputData[i]["Subcategories"] = subCategories;
else
outputData[i]["Subcategories"] = null;
}
}
} catch(error) {
console.log(error)
// Deal with the error
}
以上是如何使用 async/await
语法的示例。记得使用关键字await
,周围的函数必须标记为async
.
mysqldb.js
const mysqlDatabasePool = mysql.createPool({
connectionLimit: 50,
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USERNAME,
password: process.env.MYSQL_PASSWORD,
port: process.env.MYSQL_PORT,
database: process.env.MYSQL_DATABASE
});
mysqlDatabasePool.getConnection({
//Basic sanity checking code here
});
mysqlDatabasePool.query = util.promisify(mysqlDatabasePool.query);
module.exports = mysqlDatabasePool;
employeeController.js
var pool = require("mysqldb");
pool.query("SELECT CategoryID, CategoryIcon FROM Categories WHERE CategoryName = ?", [categoryName], function(error, results, fields) {
if(results !== null && results.length > 0) {
for(var i = 0; i < results.length; i++) {
outputData[i] = results[i];
var subCategories = pool.query("SELECT SubcategoryID FROM Subcategories WHERE CategoryID = ?", [results[i].CategoryID], function(error, results, fields) {
return results;
});
if(subCategories.length > 0)
outputData[i]["Subcategories"] = subCategories;
else
outputData[i]["Subcategories"] = null;
}
}
});
outputData[i]["Subcategories"]
的内容总是 null
而它应该有子类别的集合,因为它总是可用的。我尝试通过将 await 放在第二个 pool.query
之前并使第一个 pool.query
闭包函数异步来实现 await/async
。
应该如何处理?
作为参数传递给 pool.query
的函数是回调。这意味着您无法知道它何时会被执行。
您可以使用 async/await 来阻止调用,直到 pool.query 给您一个结果。
您可能需要在 pool.query
上使用 util.promisify
以使其 return 成为 Promise。
try {
const results = await pool.query("SELECT CategoryID, CategoryIcon FROM Categories WHERE CategoryName = ?", [categoryName]);
if(results !== null && results.length > 0) {
for(var i = 0; i < results.length; i++) {
outputData[i] = results[i];
const subCategories = await pool.query("SELECT SubcategoryID FROM Subcategories WHERE CategoryID = ?", [results[i].CategoryID]);
if(subCategories.length > 0)
outputData[i]["Subcategories"] = subCategories;
else
outputData[i]["Subcategories"] = null;
}
}
} catch(error) {
console.log(error)
// Deal with the error
}
以上是如何使用 async/await
语法的示例。记得使用关键字await
,周围的函数必须标记为async
.