我正在使用运行Express的Node.js/TypeScript创建API。以下是我的get方法的摘录。在format方法中触发了一个错误,该错误引发了由诺言捕获的错误,但在引发后不会传播到父诺言:
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!
然后,请求无法完成。
我已经阅读了很多关于Promises的文章,但找不到与我类似的问题/解决方案。
在该for循环中运行不是异步的,因此您的诺言基本上是在循环完成后立即解决,但尚未完成所有格式化。
使用一个promise控制流,例如bluebird的Promise.each,它是serial或just Promise.all。然后将捕获任何异常。
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() })