Function complains about an undefined value
Asked Answered
D

3

16

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?

Dysphoria answered 21/9, 2022 at 15:55 Comment(0)
D
13

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);
Dysphoria answered 21/9, 2022 at 15:55 Comment(3)
I would use proper settings from my answer and not building something around it. https://mcmap.net/q/726107/-function-complains-about-an-undefined-valueBetrothed
In my use-case, persisting an undefined value is considered an error, so I want DynamoDB to complain about itDysphoria
Bad idea. Your solution doesn't make "dynamoDB complain about it" as you have said. It will simply hide the real problem and store that ugly string. You probably have a very simple validation bug somewhere.Christianity
B
11

You should use the translateConfig object to specify the behaviour for marshall and unmarshall as described here:

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_lib_dynamodb.html#configuration

You can use following setting to handle undefined values.

Betrothed answered 13/12, 2022 at 14:9 Comment(0)
H
2

you can now use removeUndefinedValues: true at the second parameter of the marshall() function to remove the undefined values of your item

const input: PutItemCommandInput = {
    TableName: 'your-tablename',
    Item: marshall({item}, { removeUndefinedValues: true }),
};
Hagberry answered 28/6 at 4:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.