【发布时间】:2017-06-21 16:38:00
【问题描述】:
我正在开发一个 GET 端点来从数据库 (dynamoDB) 中获取元素。我正在使用 Swagger 在我的 api 中定义数据模型。这是我控制器中的 operationId 方法:
getInvoiceConfigById: function(req, res) {
var myId = req.swagger.params.id.value;
// InvoiceConfig is a dynamoDb model
// providerId attribute is the unique key of the db table
InvoiceConfig.scan('providerId')
.eq(myId)
.exec(function (err, config) {
if (err) {
console.log("Scan InvoiceConfig error");
throw err;
}
res.status(200).send(config);
});
}
如果找不到 id,我想发送 404 消息。 我在 swagger-ui 中注意到响应的主体是空的
Response Body
[]
当在 db 中找不到 id 时。 找不到 id 时如何在我的代码中检测到?我试图检查响应的正文是否为空:
if(!(config.body))
但这不起作用,因为正文不是空的
【问题讨论】:
标签: node.js express amazon-dynamodb swagger endpoint