As the title said, I want to put a variable into one of its Item only if the variable is not undefine.
I've tried this code.
const createNewAccount = new PutCommand({
TableName: tableName,
Item : {
"PK" : email,
"SK" : type + "#" + school,
"GSI1SK" : classroom + "#" + name,
}
});
if(type == "STD"){
createNewAccount.Item["number"] = number
}
const createNewAccountResponse = await client.send(createNewAccount);
Raise the error "Cannot set properties of undefined (setting 'number')"
at the line createNewAccount.Item["number"] = number
Also, my number variable is an integer, and all variables will not be undefine in this case.
I'm not sure is there a way to exactly solve this problem. If it has, please let me know.
Thank you!
Try this:
Here is a full example which I tested:
And the result: