Can I ask about question about stringify

Can I ask about question about stringify
0.0 0

#1

So i’ve been trying to solve why Is test console log(0,1,2,3,4,5,6,7,8,…)
in this code
`console.log(‘get game from dynamodb=’ +testing +test);

get game from dynamodb={"UserName":["Kevin"]}0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21
`

where

Data: {
    S: JSON.stringify(this.data); // in where amazon Dynamo DB it's  "UserName": ["Kevin"]
}
and 
 test = Object.keys(data.Item.Data.S)// I was expecting "UserName" so I can get "Kevin" here"...
but 
test =[0,1,2,3,4,5,6,7.....]

#2

I am not exactly sure what you are asking. Can you provide the full code? I feel like there is something missing that we can not see which will help us.

I say this because if you have the following console.log statement:

console.log(‘get game from dynamodb=’ +testing +test);

I do not see a variable called testing anywhere in your code. I do see a variable named test.


#3

I’ll try
but the testing is =(data.Item.Data.s); // it returns “UserName” : [“Kevin”];

Game.prototype = {
        save: function (callback) {
            //save the game states in the session,
            //so next time we can save a read from dynamoDB
            this._session.attributes.currentGame = this.data;
            dynamodb.putItem({
                TableName: 'UserNames',
                Item: {
                    CustomerId: {
                        S: this._session.user.userId
                    },
                    Data: {
                        S: JSON.stringify(this.data)
                    }
                }
            }, function (err, data) {
                if (err) {
                    console.log(err, err.stack);
                }
                if (callback) {
                    callback();
                }
            });
        }
    };

    return {
        loadGame: function (session, callback) {
            if (session.attributes.currentGame) {
                console.log('get game from session=' + session.attributes.currentGame);
                callback(new Game(session, session.attributes.currentGame));
                return;
            }
            dynamodb.getItem({
                TableName: 'UserNames',
                Key: {
                    CustomerId: {
                        S: session.user.userId
                    }
                }
            }, function (err, data) {
                var currentGame;
                if (err) {
                    console.log(err, err.stack);
                    currentGame = new Game(session);
                    session.attributes.currentGame = currentGame.data;
                    callback(currentGame);
                } else if (data.Item === undefined) {
                    currentGame = new Game(session);
                    session.attributes.currentGame = currentGame.data;
                    callback(currentGame);
                } else {
                    var test = Object.keys(data.Item.Data.S);
                    var testing= data.Item.Data.S;
                    console.log('get game from dynamodb=' +testing +test);// here is the console.log
                    currentGame = new Game(session, JSON.parse(data.Item.Data.S));
                    session.attributes.currentGame = currentGame.data;
                    callback(currentGame);
                }
            });
        },
        newGame: function (session) {
            return new Game(session);
        }
    };
})();
module.exports = storage;

So I was thinking about Jsonstringify (I’m still new to Coding) so it’s suppose to return " " on things


#4

What are you expecting it to display?


#5

sorry, where is the duplicate question maybe I double click it …

I was expecting
if testing is = data.Item.Data.S ; // “UserName”: [“Kevin”];

test is =Object.keys(data.Item.Data.S);// "UserName"
so If I console.log( “…” + testing[test]) // I would get "Kevin"
and it return undefine so when I change it to (testing + test) I found out that it’s ,0,1,2,3,4,… and so on


#6

Can you modify your code as follows and then copy/paste what you see in the console?

                } else {
                    console.log(data.Item);
                    var test = Object.keys(data.Item.Data.S);
                    var testing= data.Item.Data.S;
                   // console.log('get game from dynamodb=' +testing +test);// here is the console.log
                    currentGame = new Game(session, JSON.parse(data.Item.Data.S));
                    session.attributes.currentGame = currentGame.data;
                    callback(currentGame);

#7

I did, but I think I can’t show you the Amazon.echo.ID… …
but

Item: {
= my Amazon account
}
Data:{
then the tabel


#8

Just replace any personal identifying information with xxxxxxxxxxxx, but I still need to see the entire structure and contents of data.Item.


#9

it’s Data: { S: ‘{“UserName”:[“Kevin”]}’ }
CustomerId: { S: xxxxxxxxxxxxxxxxx}
request end:


#10

I can not tell if data.Item is an array or an object. I still feel like you are not showing the correct syntax for data.Item

Is it the following?

{
  Data: {S: '{UsernName":["Kevin]}' },
  CustomerId: { S: xxxxxxxxxxxx}
}

#11

Yes it’s

I just copy paste this…
{ Data: { S: ‘{“UserName”:[“Kevin”]}’ },
CustomerId: { S:xxxxxx}
END RequestId: xxxxxx


#12

The first time you where missing commas, so I could not tell what you had.


#13

also I did try it repl.it

and if

var testing = '{"Username":["kevin"]}',

test =Object.keys(testing);
var newtest = testing[test];
console.log(newtest);

it return = undefined.


#14

hmmm maybe I should un string it first ? then get the Object.keys?


#15
var userName = JSON.parse(Item.Data.S).UserName[0];
console.log(userName); // 'Kevin'

The above code assumes there would only be one name in the array of the UserName property.


#16

that’s so COOL man so
If the user said add sarah it will return “ok Kevin” instead of "Ok sarah"
Ok let’s try this out.


#17

randell, I’ll let you know tomorrow on what happen it’s 6 10 got to go home


#18

randell, Thanks so much Man, finish everything about my work, and now
I just need to improve or know How to use SSML more… specially phoneme

I just your advice Json.parse to unstring it then call it then String it again then boom

Also the skill is randomize Positive By the way Called ARK(One ack of random kindness),
I know that it’s to much to ask right now specially that I didn’t give you something in return,
but if you want to try and test the skill, I’ll send you a link after everything is done.
(in Alexa)
Much love and Be Blessed

KC> Diez. L.E.A.F