node.js - 嵌套 Promise 不会将错误传播到 Node.js 中的父 Promise?

标签 node.js express typescript promise sequelize.js

我正在使用运行 Express 的 Node.js/TypeScript 创建一个 API。以下是我的 get 方法的摘录。在 format 方法中触发了一个错误,该错误会抛出一个由 Promise 捕获的错误,但在抛出后不会传播到父 Promise:

            this.getModel(objectName).findAll(queryParameters).then(function(databaseObjects) {
                for (let databaseObject of databaseObjects) {
                    var jsonObject = {};
                    //console.log("Database object: ");
                    //console.log(databaseObject);
                    transform.baseFormat(databaseObject, jsonObject)
                    .then(() => transform.format(databaseObject, jsonObject))
                    .then(() => {
                        res.locals.retval.addData(jsonObject);
                    }).catch((e) => {
                        console.log("Caught error during format of existing object: ");
                        console.log(e);
                        throw e;
                    });
                }
            })
            .then(() => {
                if (metadata) {
                    this.metadata(objectName, false, transform, res.locals.retval);

                    delete queryParameters.limit;
                    delete queryParameters.offset;
                    console.log("RUNNING METADATA COUNT: ");
                    this.getModel(objectName).count(queryParameters).then(function(count) {
                        res.locals.retval.setMetadata("records", count);
                        return next();
                    }).catch(function(e) {
                        this.error(e, res);
                        return next();
                    });
                } else {
                    console.log("NO METADATA");
                    return next();
                }
            })
            .catch((e) => {
                // TODO: Move status into error() function
                console.log("500 Error on GET");
                console.error(e);
                res.locals.retval.addError(ErrorCode.InternalError, e);
                res.status(ErrorCode.InternalError).send(res.locals.retval);
                return next();
            });

这是输出:

(node:8277) Warning: a promise was created in a handler at /Library/WebServer/adstudio/dist/server.js:555:51 but was not returned from it, see
at Function.Promise.bind (/Library/WebServer/adstudio/node_modules/bluebird/js/release/bind.js:65:20)
Caught error during format of existing object: 
Test Error
END FUNCTION HAS BEEN REACHED!

然后请求无法完成。

我读了很多关于 Promise 的文章,但一直没能找到与我类似的问题/解决方案。

最佳答案

在 for 循环内运行不是异步的,因此您的 promise 基本上在循环完成后立即解决,但在所有格式化完成之前。

使用 Promise 控制流,如 bluebird 的 Promise.each它是串行的或只是 Promise.all。然后任何异常都会被捕获。

this.getModel(objectName).findAll(queryParameters).then(function (databaseObjects) {
  var promises = databaseObjects.map(databaseObject => {
    var jsonObject = {}
          // console.log("Database object: ");
          // console.log(databaseObject);
    return transform.baseFormat(databaseObject, jsonObject)
          .then(() => transform.format(databaseObject, jsonObject))
          .then(() => {
            res.locals.retval.addData(jsonObject)
          }).catch((e) => {
            console.log('Caught error during format of existing object: ')
            console.log(e)
            throw e
          })
  })
  return Promise.all(promises)
})
.catch((e) => {
    // TODO: Move status into error() function
  console.log('500 Error on GET')
  console.error(e)
  res.locals.retval.addError(ErrorCode.InternalError, e)
  res.status(ErrorCode.InternalError).send(res.locals.retval)
  return next()
})

关于node.js - 嵌套 Promise 不会将错误传播到 Node.js 中的父 Promise?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48119285/

相关文章:

javascript - 我的日期函数出现 Node JS 错误,出了什么问题?

Node.js 运行 java 程序?

javascript - 如何更改另一个组件属性的值? ( Angular 2)

javascript - 在 promise 中处理错误时返回成功

angularjs - 如何定义注入(inject)动态组件的位置?

javascript - 错误!安装无法读取 mac 上的依赖项

node.js - 如何强制 npm 使用 https Node 存储库

node.js - 在终端关闭后保持 ExpressJS 应用程序运行

node.js - Node 乘法器 : Form submission fails when there is no file to upload

node.js - 在 Express js/中从域重定向到子域时如何保持 session 事件