0

I'm writing a node program that does the below.

  • Scan the DynamoDB and get the result.
  • Based on the result do the below.
    • if the returned value is of size 1. console.log the value.
    • If the result is more than 1 concat the result and the print it to the console. Currently I'm able to do get the result and print it in console. if it is greater than one, I'm confused on how can I do this.

Below is my code.

function getMyDueResponses(response, session) {
var responseText='';
  console.log('Here is your result' + session.attributes.userData.Count);
  console.log(session.attributes.userData.length);
  if (session.attributes.userData.Count < 2) {
    var res = session.attributes.userData;
    var userDueDate = JSON.stringify(res.Items[0].dueDate);
    var userDueAmount = JSON.stringify(res.Items[0].dueAmount);
    var userTotalBalance = JSON.stringify(res.Items[0].totalBalance);
    responseText = `Your next Chubb bill is due on ${userDueDate}. The payment due is ${userDueAmount}$. The full account balance is ${userTotalBalance}$.`;
    console.log(responseText);
}
  else {
    //Here I'm stuck on how to proceed.
    }
}

for example. the DB returned the below data.

{
    "Items": [
        {
            "accountId": "12345",
            "pin": "1234",
            "userId": "user1",
            "dueDate": "5/20/2017",
            "_id": "2",
            "dueAmount": "4000",
            "totalBalance": "10000"
        }
    ],
    "Count": 1,
    "ScannedCount": 4
}

the response should be. I'm able to get this result.

Your bill is due on 5/20/2017. The payment due is 4000$. The full account balance is 10000$.

the db returned the below data.

{
    "Items": [
        {
            "accountId": "12345",
            "pin": "1234",
            "userId": "user1",
            "dueDate": "5/20/2017",
            "_id": "2",
            "dueAmount": "4000",
            "totalBalance": "10000"
        },
        {
            "accountId": "12345",
            "pin": "1234",
            "userId": "user1",
            "dueDate": "5/23/2017",
            "_id": "2",
            "dueAmount": "1000",
            "totalBalance": "10000"
        },
        {
            "accountId": "12345",
            "pin": "1234",
            "userId": "user1",
            "dueDate": "5/24/2017",
            "_id": "2",
            "dueAmount": "300",
            "totalBalance": "10000"
        }
    ],
    "Count": 3,
    "ScannedCount": 4
}

the below data has to be printed in console.log().

Your bill 1 is due on 5/20/2017. The payment due is 4000$. The full account balance is 10000$. Your bill 2 is due on 5/23/2017. The payment due is 1000$. The full account balance is 10000$. Your bill 3 is due on 5/24/2017. The payment due is 300$. The full account balance is 10000$.

please let me know how can I do this.

1 Answer 1

2

Why don't you just run a loop for the amount of items?

EDIT: You could add every response to an array while looping, and afterwards print the array as a single string with Reduce();

    var arr = [];
    var res = session.attributes.userData;


    for (var x = 0; x < session.attributes.userData.Count; x++) {

        var userDueDate = JSON.stringify(res.Items[x].dueDate);
        var userDueAmount = JSON.stringify(res.Items[x].dueAmount);
        var userTotalBalance = JSON.stringify(res.Items[x].totalBalance);
        responseText = `Your next Chubb bill is due on ${userDueDate}. The payment due 
        is ${userDueAmount}$. The full account balance is ${userTotalBalance}$.`;
        arr.push(responseText);
    }

console.log(arr.reduce(function(acc, cur) { return acc + cur; }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick suggestion, This doesn't work as per my case. I know that looping will do the job. But I want to console print after the looping is done... I heard that this thing is achievable using promises but I'm not sure of how this thing works. Can you please help me out on this?
I edited my answer with the use of Reduce, maybe this is what you need?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.