-2

I am new to dynamoDB and node.js I have written a code where it will make a query to the database (dynamodb) and look for an element which is entered by the user in the database. I am able to verify that but when the user tries with some other number which is not present in the database I am getting a null value.

My table name is "DevReferenceNumber" and only one column which is primary key "referencenumber".

    'use strict';

    var AWS = require('aws-sdk');

    var docClient = new AWS.DynamoDB.DocumentClient({ region : 'us-east-1'});


    function close(sessionAttributes, fulfillmentState, message) {
        return {
            sessionAttributes,
            dialogAction: {
                type: 'Close',
                fulfillmentState,
                message,
            },
        };
    }


    exports.handler = (event, context, callback) => {

    try{

      console.log(`event.bot.name=${event.bot.name}`);

      if(event.bot.name != 'getCustomerReferenceNumber'){
          callback('Invalid Bot Name');
        }

      dispatch(event, (response) => {

          callback(null, response)

        });

    }catch(err){


        callback("Error is occured while querying");

    }

   };
   function dispatch(intentRequest, callback){

    console.log(`dispatch UserID => ${intentRequest.userId}, intentName => ${intentRequest.currentIntent.name}`);

    const intentName = intentRequest.currentIntent.name;

        if(intentName === "checkReferenceNumber"){
        return referenceNumber(intentRequest, callback);

        }



}
function referenceNumber(intentRequest, callback){

    const enteredReferenceNumber = intentRequest.currentIntent.slots.ReferenceNumber;
    const sessionAttributes = intentRequest.sessionAttributes;

    console.log("User has entered reference number is --> " + enteredReferenceNumber);

    var params = {

        TableName : "DevReferenceNumber",
        KeyConditionExpression: "#refer = :ref",
        ProjectionExpression : "#refer",
        ExpressionAttributeNames: {
            "#refer" : "referencenumber"
        },
        ExpressionAttributeValues:{
            ":ref": parseInt(enteredReferenceNumber)
        }
    };

    docClient.query(params, function(err, data){


        if(err){

            callback(close(sessionAttributes, 'Fulfilled', {
                    contentType: 'PlainText',
                    content : 'Developer reference number is not matched with data from database'}));
        }

        else  {

            data.Items.forEach(function (item){

                console.log("User matched data is ==> " + item.referencenumber);

                callback(close(sessionAttributes, 'Fulfilled', {
                    contentType: 'PlainText',
                    content : 'Developer reference number is matched with data from database'}));
            });
        }



    });
}
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470

1 Answers1

1

It is obvious that you will get a null record when you don't have a matching record. If you don't want null from node callback then you can do a custom logic to do a null check and return data according to the way you want.

Vijayanath Viswanathan
  • 8,027
  • 3
  • 25
  • 43
  • when i went through document it mention that whenver there is not element found it will return a empty set. i tried putting condition like if (data.length ==0) then do callbacks but still it's printing null. Can you help me with some example code. – Anand Kumar Oct 24 '17 at 03:54
  • i got the answer i checked with one of the properties with the empty set. Thanks – Anand Kumar Oct 24 '17 at 04:31