简体   繁体   中英

NodeJS API GET by Id. How to detect item not found?

I'm developing a GET endpoint to fetch elements from a database(dynamoDB). I'm using Swagger to define the data model in my api. This is the operationId method in my controller:

 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);

                 });
 }

I would like to send a 404 message if the id was not found. I've noticed in swagger-ui that the body of the response comes empty

Response Body

[] 

when the id is not found in the db. How can I detect in my code when id was not found? I've tried to check if the body of the response is empty:

if(!(config.body))

but this doesn't work because the body is not null

You can check the count of config at server side, if config count is 0 the send desire response code 404 else return 200 response code with data as shown below

 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);
                   }
                 });
 }

Try adding a length check in your callback, like so:

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();
               }
             });
}

I would also suggest that you should simply use the getItem query instead of scan:

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

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

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM