I have generated an app using swagger-node ( https://github.com/swagger-api/swagger-node ).
The automatic swagger router is not recognizing controller middleware functions with an async prefix. I have to use Promises inside middlewares.
Is there a way to use async in the middlewares which are listed in swagger.yaml ?
in swagger.yaml:
paths:
'/positions/{positionId}':
x-swagger-router-controller: controller
get:
description: some description
operationId: getPosition
in controllers/controller.js
module.exports.getPosition = function(request, response) {
const positionId = request.swagger.params.positionId.value;
try {
someModel.getPosition(positionId)
.then(function() {
return response.status(200).json();
})
.catch(function(error) {
return response.status(500).json();
});
} catch(error) {
return response.status(500).json();
}
};
and I would like to write:
module.exports.getPosition = async function(request, response) {
const positionId = request.swagger.params.positionId.value;
try {
await someModel.getPosition(positionId);
return response.status(200).json();
} catch(error) {
return response.status(500).json();
}
};
This is an issue of the swagger-node-runner
package (see here ) :
they need to they'd need to update their code to use the latest release of
swagger-tools
.
I fixed that by :
First, installing the 0.10.3
version of swagger-tools
in the project :
npm install --save swagger-tools@0.10.3
Then, changing the dependency of swagger-node-runner
manually in the node_modules/swagger-node-runner/package.json
file, like :
[...]
"dependencies": {
"bagpipes": "^0.0.6",
"config": "^1.16.0",
"cors": "^2.5.3",
"debug": "^2.1.3",
"js-yaml": "^3.3.0",
"lodash": "^3.6.0",
"swagger-tools": "^0.10.3"
},
[...]
For you to use the wait, you are using async in the creation of the function, it follows:
module.exports.getPosition = async function(request, response) { const positionId = request.swagger.params.positionId.value; try { await someModel.getPosition(positionId); return response.status(200).json(); } catch(error) { return response.status(500).json(); } };
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.