When I try to persist my JavaScript object to DynamoDB using the PutCommand, I am seeing the following error message:
Error: Pass options.removeUndefinedValues=true to remove undefined values from map/array/set.
This happens when I use DynamoDBDocumentClient
When I use DynamoDBClient then I must first marshal the object using marshall(..) from @aws-sdk/util-dynamodb. In this case the error is shown when I try to marshal the object.
When I print my object to the console, I don't see any undefined values. However, I don't see the complete object due to too many levels of nesting:
{ id: 123, child: { anotherChild: { nested: [Object] } } }
So instead I use JSON.stringify(..) to display the entire object:
{
"id": 123,
"child": {
"anotherChild": {
"nested": {
"name": "Jane"
}
}
}
}
I apparently don't have any undefined attributes, so why am I seeing the error message?
The marshal(..) function will throw this error if it encounters an attribute that is undefined.
marshall({
name: "John",
age: undefined,
})
My object did have an undefined value, but it turns out that JSON.stringify(..) will remove attributes with an undefined value.
I had to add a "replacer function" to JSON.stringify(..) to see (and fix) the undefined value.
import {marshall} from "@aws-sdk/util-dynamodb";
const myObject = {
id: 123,
child: {
anotherChild: {
nested: {
name: "Jane",
age: undefined,
},
},
},
};
// Doesn't show the undefined value (too many levels of nesting)
console.log(myObject);
// Doesn't show the undefined value (silently removes undefined attributes)
console.log(JSON.stringify(myObject));
// DOES show the undefined value
console.log(JSON.stringify(myObject, (k, v) => v === undefined ? "!!! WARNING UNDEFINED" : v));
// The marshall function throws an error due to the presence of an undefined attribute
marshall(myObject);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With