【问题标题】:NodeJS API GET by Id. How to detect item not found?NodeJS API 按 ID 获取。如何检测未找到的项目?
【发布时间】: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


    【解决方案1】:

    您可以在服务器端检查config的计数,如果配置计数为0,则发送期望响应码404,否则返回200响应码,数据如下所示

     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;
                       }
                       if (config.length == 0){
                          res.status(404).end();
                       } else {
                          res.status(200).send(config);
                       }
                     });
     }
    

    【讨论】:

    • 怎么样?你的意思是 config.body.length 吗?
    • 我的意思是在扫描方法的回调中,您检查长度并根据...发送响应...
    【解决方案2】:

    尝试在回调中添加长度检查,如下所示:

    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;
                   }
    
                   if(typeof config === 'array' && 0 < config.length){
                     res.status(200).send(config);
                   } else {
                     res.status(404).send();
                   }
                 });
    }
    

    我还建议您应该简单地使用 getItem 查询而不是扫描:

    http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#getItem-property

    【讨论】:

      【解决方案3】:

      由于config 对象键值在未找到结果时为 1,因此您可以像这样检查该对象的键长度:

      if ( Object.keys(config).length == 1 )return res.status(400).send("Error 404"); 
      

      【讨论】:

      • 这将打印“1” console.log(Object.keys(config).length);当没有找到 id 时,否则为“2”
      • @rodrunner 所以使用那个条件!我根据你告诉我的编辑了答案
      猜你喜欢
      • 2020-06-10
      • 2013-04-23
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多