Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Cannot convert undefined or null to object' when updating attribute to null or empty array #276

Closed
chrisbrantley opened this issue Jan 30, 2020 · 3 comments
Assignees
Labels

Comments

@chrisbrantley
Copy link

chrisbrantley commented Jan 30, 2020

Describe the bug
I'm trying to update some attributes to null or [] (empty array) and I'm getting the following error:

TypeError: Cannot convert undefined or null to object
    at Function.getOwnPropertyNames (<anonymous>)
    at Object.toDb (@shiftcoders/dynamo-easy/src/mapper/mapper.ts:56:48)
    at Object.objectToDb [as toDb] (@shiftcoders/dynamo-easy/src/mapper/for-type/object.mapper.ts:23:13)
    at Object.toDbOne (@shiftcoders/dynamo-easy/src/mapper/mapper.ts:140:14)
    at buildDefaultExpression (@shiftcoders/dynamo-easy/src/dynamo/expression/update-expression-builder.ts:112:19)
    at buildUpdateExpression (@shiftcoders/dynamo-easy/src/dynamo/expression/update-expression-builder.ts:62:10)
    at wrapper (lodash/lodash.js:5193:19)
    at @shiftcoders/dynamo-easy/src/dynamo/expression/prepare-and-add-update-expressions.function.ts:24:16
    at Array.map (<anonymous>)
    at Object.prepareAndAddUpdateExpressions (@shiftcoders/dynamo-easy/src/dynamo/expression/prepare-and-add-update-expressions.function.ts:23:8)

To Reproduce

@Model({ tableName: "my-table" })
class TestModel {
  @PartitionKey()
  pk: string;

  @SortKey()
  sk: string;

  myAttribute: string | null;
}

const store = new DynamoStore(TestModel);

await store
      .update("my-primary-key", "my-sort-key")
      .updateAttribute("myAttribute")
      .set(null);

Expected behavior
Since both Null and empty arrays are valid DynamoDB values I would expect the operation to succeed.

Additional context
This error only happens with update operations. I can call store.put() with the same values and it works correctly.

I am using Typescript.

@simonmumenthaler
Copy link
Contributor

Hi @chrisbrantley
Thanks for opening the issue and to provide the code to reproduce it.

This fail happens due to the inability of dynamo-easy to handle null values -> it filters out all null/undefined/empty values.

We will address this in the near future and bring null-value support to dynamo-easy; see #285
stay tuned!

@fredspivock
Copy link

I need to update an array with an empty array and get the same error. Setting something to an empty array seems like a valid usecase.

@hookercookerman
Copy link
Contributor

hookercookerman commented Sep 7, 2020

I am facing the same issue as @fredspivock often you want to have lists of objects that can be added and removed, via updating, and a very common scenario is setting that list back to being empty. Removing is not really an option as you want that empty array so you can append objects.

so just to explain further this call does not work and results in Cannot convert undefined or null to object which comes from
object mapper toDb call.

example would be in terms of update operations

    const operation = update2(Breakfast, 'eggs').set([]);
    // this currently does not work it kind of should

happy to do make this change if you can point me to a good starting point.

Problem

its here that an empty array will would filtered via the deepfiler call which would always return [] for any empty array instead
of [[]]

https://github.com/shiftcode/dynamo-easy/blob/master/src/dynamo/expression/update-expression-builder.ts#L40

without knowing the full details of deepfilter and why it rejects empty array I cannot really deep dive into a fix here. I can make one but it will be hack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants