Dynamoose: Incorrect response for Object type

Created on 8 Sep 2018  路  10Comments  路  Source: dynamoose/dynamoose

Summary:

Have a schema entry :
PayableAmount: {type: Map}
have also tried
PayableAmount: {type: Object},

Code sample:

Schema

const schema = {
Account: {type: String, hashKey: true}
IssueDate: {type: String}
PayableAmount: {type: Object}
}

Model

Model has no extra behavior apart from having a schema

General

Model.query({
      Account: {eq:"someID"},
      IssueDate: { le: "Date1", ge: "Date2},
    }).exect(callbackFunc);

Current output and behavior:

Query works when I look in debugger and I get the expected return value from dynamo. However, parsing the result for PayableAmount (type M) tries to read it as a type S and throws the following error:

SyntaxError: Unexpected token o in JSON at position 1
at parse ()
at dedynamofy (/development/server/node_modules/dynamoose/lib/Attribute.js:470:14)
at Attribute.parseDynamo (/development/server/node_modules/dynamoose/lib/Attribute.js:540:13)
at Schema.parseDynamo (/development/server/node_modules/dynamoose/lib/Schema.js:189:24)
at toModel (/development/server/node_modules/dynamoose/lib/Query.js:240:18)

Expected output and behavior:

Should return the Map/Object

Environment:

Operating System: OSX
Operating System Version: 10.13.6
Node.js version (node -v): v10.8.0
NPM version: (npm -v): 6.4.0
Dynamoose version: "0.8.7"

Other information (if applicable):

Type (select 1):

  • [ ] Bug report
  • [ ] Feature suggestion
  • [ ] Question
  • [ ] Other suggestion
  • [ ] Something not listed here

Other:

  • [x] I have read through the Dynamoose documentation before posting this issue
  • [x] I have searched through the GitHub issues (including closed issues) and pull requests to ensure this issue has not already been raised before
  • [x] I have searched the internet and Stack Overflow to ensure this issue hasn't been raised or answered before
  • [x] I have tested the code provided and am confident it doesn't work as intended
  • [x] I have filled out all fields above
  • [x] I am running the latest version of Dynamoose
bug

All 10 comments

After looking into this issue more it looks like the problem is that you have not set useDocumentTypes to true in your schema. By default Dynamoose will use string based JSON for document based saving and reading data from DynamoDB. This is due to DynamoDB previously not supporting native lists and maps. You can toggle that option on by setting it in your schema settings, see the documentation here (the useDocumentTypes property).

By default this option is set to false, but the default will be changed to true once PR #376 gets merged in. Currently that PR has failing tests so those need to be fixed before it can be merged in.

I'm closing this issue and related PR for now, since I believe this is working as expected. If toggling that option doesn't work or you have a strong argument for why we should change this behavior please comment and I can help further.

Hope this helps!!

I've tried this and set useDocumentTypes but still get the same error.

@fishcharlie SyntaxError: Unexpected token o in JSON at position 1
The issue happens when another service saves data to dynamo, but I've seen it happen a few different ways. It's critical that dynamo detect that it gets a non-JSON response back that it handles this gracefully. Dynamoose CANNOT depend on being responsible for all saves and all returns all the time.

@brennhill I 100% agree about Dynamoose not depending on being responsible for all saves and all returns all the time. Let me see if I can find some time to do more investigation into this and then I'll get back to you.

@brennhill Two things, can you try to check to see if this is still an issue in Dynamoose v1.0.0. Also, if that doesn't work can you send the DynamoDB JSON for the item that is causing the error?

Now I am getting empty object ( {} ) in version 1.0.0 for keys which are declared as type : Object and useDocumentTypes for the schema is set to true.

@jignesh248 Please see the latest comment in #434. I'm going to be using that issue to track this bug.

@jignesh248 If you want to comment on #434 to help debug that problem, that would be really appreciated.

@brennhill Did you get a chance to look into https://github.com/dynamoosejs/dynamoose/issues/421#issuecomment-428032773?

@brennhill Is there any chance you can create a failing test case for this, then submit a PR for that? The PR will be marked as failing the tests. Once you do that, I will checkout that PR, and do some further investigation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chasevida picture chasevida  路  3Comments

MickL picture MickL  路  7Comments

androidand picture androidand  路  3Comments

MickL picture MickL  路  6Comments

craigsketchley picture craigsketchley  路  5Comments