dynamodb
is being very indecisive for me. I have a random id generator that includes both letters and numbers, but when I use putItem
an error appears saying: ValidationException: The parameter cannot be converted to a numeric value: random3string8of5letters4and2numbers
.
If I give in and decide to make my id generator number only, it appears with another error: ValidationException: One or more parameter values were invalid: Type mismatch for key user_id expected: S actual: N
if I put user_id: "999999"
I am very confused right now. Any help would help :)
Function where weird stuff is happening:
const dynamo = new AWS.DynamoDB();
async function addUser(username, password, email, firstName) {
const items = {
"user_id": {
"N": 2 //or "2" or "j38873kjhad8123" or something like that
//normally r() - the random id generator that returns a random id
},
"username": {
"S": username
},
"password": {
"S": password
},
"email": {
"S": email
},
"firstName": {
"S": firstName
}
};
const message = new Promise((res, rej) => {
dynamo.putItem({
TableName: 'user_data',
Item: items
},
(err, data) => {
if (err) rej(err);
else res({ id: user_id, message: `User added to database with user_id ${user_id}` });
});
});
return message;
}
//example usage of function:
addUser("test", "test", "test", "test").then(...);
random id generator:
function r() { //get randomized string 30 chars long
let res = '';
const chars = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"];
for (let i = 0; i < 30; i ) {
res = chars[Math.floor(Math.random() * chars.length)];
}
return res;
}
CodePudding user response:
There are two places that you need to indicate that user_id
is a string:
- in the attribute definitions when creating your table
- in the item you are passing to the putItem call
It looks like your table definition was correctly configured with user_id
as a string so the problem must be in the parameters to your putItem call. Use the following:
"user_id" : { "S": r() }