0

I am working on a Node.js server for a game I have created. The server acts as an api to retrieve user account info including their personal high scores. I have an individual item for each account which contains their username and a list of their scores for the game. I can't seem to find any example code for inserting values into a list in DynamoDB and I was hoping someone could help me with this. I'm not finding documentation for this specific action. I have simply been trying to use the putitem function that I know works for inserting strings and other data types, but I have had no luck with inserting into a list data type. My most recent attempt at this looks like the following:

var params = {
    TableName : "userscores",
    Item:{
        "username" : {"S": username},
        "scores" : {"L": {"S": score}}
    }
}
dynamodb.putItem(params, function(err, data) {
    if(err)
        console.log(err); 
    else 
        console.log(JSON.stringify(data)); 
}); 

The error looks like this: { [InvalidParameterType: Expected params.Item['scores'].L to be an Array] message: 'Expected params.Item[\'scores\'].L to be an Array', code: 'InvalidParameterType'

I understand why this block doesn't work, but I don't understand how to do this. Any help would be greatly appreciated!

2 Answers 2

2
let data = {
  "username" : "name",
  "scores" : ["val1", "val2"]
};
let attrs=toAttributes(data);

dynamodb.putItem({
  TableName: "underscored",
  Item: attrs,
  // not needed for you example, just some extra info
  ConditionExpression: 'attribute_not_exists(#id)',
  ExpressionAttributeNames: {'#id': '<aUniqueColumnNameIfYouUse>'}
});

toAttributes = obj => {
  return Object.keys(obj).reduce((prevResult, sKey)=> {
    prevResult[sKey] = getAttributeFromValue(obj[sKey]);
    return prevResult
  }, {});
};

getAttributeFromValue = value => {
  var type = typeof value;

  if (type === "string") {
    return { S: value };

  } else if (type === "number") {
    return { N: value.toString() };

  } else if (type === "boolean") {
    return { BOOL: value };

  } else if (value.constructor === Array) {
    var array = value.map(function(element) {
      return getAttributeFromValue(element);
    });
    return { L: array }

  } else if (type === "object" && value !== null) {
    var map = {};
    for (var key in value) {
      map[key] = getAttributeFromValue(value[key]);
    }
    return { M: map }

  } else {
    return null
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can change the scores value like this:-

If scores is defined as String variable:-

var scores = "s1";
var params = {
    TableName : "userscores",
    Item:{
        "username" : {"S": username},
        "scores" : [scores]
    }
}

If scores is defined as array:-

var scores = ["s1"];
var params = {
    TableName : "userscores",
    Item:{
        "username" : {"S": username},
        "scores" : scores
    }
}

2 Comments

I tried the first version of this solution, and got an error. I am indeed attempting to add a score variable to a dynamodb list. When I try this I get the error: { [UnexpectedParameter: Unexpected key '0' found in params.Item['scores']] message: 'Unexpected key \'0\' found in params.Item[\'scores\']', code: 'UnexpectedParameter', time: Wed Nov 30 2016 07:18:55 GMT-0800 (Pacific Standard Time) }
I think you have something different from what is mentioned in the solution. Could you show your variable and assignment or full code? By any chance, have you defined the scores as key (i.e. sort or hash key)? If yes, the primary key attribute must be defined as type string, number, or binary.

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.