使用 Node 和 mssql提取五个不同数据库的查询,以相互消除重复并合并到更统一的架构中。
我的流程遵循以下算法:
通过调用此函数创建共享池:
const getPoolConnection = async () => {
try {
let pool = await mssql.connect(`mssql://${username}:${password}@${server}/`);
return pool;
} catch (err) {
console.error(err);
}
};
该函数创建池并将其返回给调用函数。 用户名
、密码
和服务器
已导入并限定到此文件。
然后我们查询每个数据库并将结果分配给对象的属性。这是通过 forEach 循环完成的:
lists.forEach(list => {
fullData[list] = db.queryDatabase(pool, customers[list].query).catch(err => console.error(err));
})
调用此函数:
const queryDatabase = async (pool, query) => {
try {
let result = await pool.request().query(query);
// console.log(result);
return result.recordset, pool;
} catch (err) {
console.error(err);
}
};
现在,为了防止在所有数据库调用返回数据之前发生后处理,我将整个调用集包装在主 中的
文件。这是调用函数:Promise.all()
调用中index.js
const { customers } = require('./query');
const util = require('./util');
const db = require('./db');
fullData = {};
(async () => {
let pool = await db.getPoolConnection();
let lists = Object.keys(customers);
Promise.all(
lists.forEach(list => {
fullData[list] = db.queryDatabase(pool, customers[list].query).catch(err => console.error(err));
})
)
.then(results, pool => {
console.dir(results);
db.closePoolConnection(pool);
})
.catch(err => console.error(err));
})();
我不明白的是尝试调试应用程序时发生的错误:
(node:18908) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined warning.js:18 at Function.all () at c:\Users\rutherfordc\Documents\GitHub\migration-plus\index.js:10:11 at at process._tickCallback (internal/process/next_tick.js:188:7) (node:18908) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) warning.js:18 (node:18908) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. warning.js:18 (node:18908) UnhandledPromiseRejectionWarning: ReferenceError: results is not defined warning.js:18 at c:\Users\rutherfordc\Documents\GitHub\migration-plus\index.js:15:11 at at process._tickCallback (internal/process/next_tick.js:188:7) (node:18908) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
最佳答案
Promise.all()需要一个数组。所以你可以这样做:
fullData = [];
(async () => {
let pool = await db.getPoolConnection();
let lists = Object.keys(customers);
lists.forEach(list => {
fullData.push(db.queryDatabase(pool, customers[list].query).catch(err => console.error(err));
}));
Promise.all(fullData)
.then((results, pool) => {
console.dir(results);
db.closePoolConnection(pool);
})
.catch(err => console.error(err));
})();
更新:
正如 J. Pichardo 在他的回答中所建议的,如果有多个参数,参数应括在括号中。
Documentation
关于javascript - 未处理的 promise 拒绝在哪里?我怎样才能避免它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51103861/