javascript - 未处理的 promise 拒绝在哪里?我怎样才能避免它?

标签 javascript sql-server node.js promise

使用 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/

相关文章:

javascript - 从 DataTables 渲染函数返回 jQuery 对象

javascript - javascript中递归编程中的变量问题

sql-server - 为什么我的 SSIS 包在第一次运行时会失败,但在第二次运行时不会失败? "Object cannot be cast from DBNULL to other types"

javascript - PromiseJS 中的延迟 promise

JavaScript 构造函数的实例是函数

javascript - Mocha 测试用例 - 嵌套 it() 函数是否符合规定?

c# - 如何返回邮政编码列表 ## 英里半径内的所有实例

sql-server - Visual Studio 2013 Ultimate 中的 SQL Server 对象资源管理器在哪里?

JavaScript 日期对象在浏览器中包含 "T"字符,但在 MongoDB 中不包含

node.js - Socket.IO 房间功能