在该for循环中运行不是异步的,因此您的诺言基本上是在循环完成后立即解决,但尚未完成所有格式化。
使用一个promise控制流,例如bluebird的Promise.each,它是serial或just
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()})


