0
votes

I am trying to read data from dynamoDB and let Alexa speak it. I am successfully retrieving the data, it's just that I'm having trouble with the JSON part (I'm new to it and I can't find the answer online).

Here's the function I am using to get the data (from https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.03.html):

var x = DBClient.get(params, function(err, data) {
    if (err) {
        console.error("Unable to read item. Error JSON:", 
        JSON.stringify(err, null, 2));
    } else {
         console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
    } });

And here is how I'm logging the variable: console.log("You can expect " + x);

In the function logs, I'm basically getting [object Object] for x.

I tried doing x.Item.Answer but that didn't work (Answer is the attribute name for the data that I'm trying to fetch)

I tried to use JSON.parse on x, but I got errors

I also tried JSON.stringify, which returned as an error "Converting circular structure to JSON" since I guess the function "get" (which i cannot find any documentation on) returns an already stringified version?

I don't know what to try anymore ...

As a note, here's the JSON that I'm (successfully) returning from the database:

{
    "Item": {
        "Answer": "Sunny weather",
        "Question": "What is the weather like today"
    }
}  
1
the variable "x" is an object of db connection. It is not the response that u get from db. the response is present in "data" attribute.rohitwtbs
did ur problem get solved?rohitwtbs
Hey. Thank you for pointing that out. So I think what you are trying to get at is similar to the answer below (please correct me if I'm wrong). I tried doing as suggested below but I got: TypeError: Cannot read property '0' of undefinedAn Ignorant Wanderer

1 Answers

0
votes
    DBClient.get(params, function(err, data) {
    if (err) {
        console.error("Unable to read item. Error JSON:", 
        JSON.stringify(err, null, 2));
    } else {
         console.log("GetItem succeeded:", data.Items[0].answer);
         //buisness logic
    } }); 

try the above code.Whenever we get a response from Dynamodb, lets say u get data in response then data object will contain Items attribute which is an array of items that u wanted to get.