[BUG] Type Mismatch error message on hashKey when unrelated field is null
See original GitHub issueSummary:
Thanks for your great work with dynamoose!
I’ve been porting over a codebase from dynamoose 1 -> 2, and I noticed a weird issue.
Writing a document with a null field and a schema with saveUnknown: true
, then querying it causes a "Type Mismatch’ error that complains about the hashKey:
UnhandledPromiseRejectionWarning: TypeMismatch: Expected hashKey to be of type string, instead found type object.
Removing the null field or removing saveUnknown: true
causes the query to complete normally.
There’s two aspects to this as far as I can tell:
- It’s unclear whether having a field that is null with
saveUnknown
is a problem - The type mismatch error blames a seemingly unrelated hashKey.
I’ve created a minimal-ish reproduction of the problem, using dynamodb local.
Clone the repo, npm install
then run npm test
to see the problem.
Code sample:
https://github.com/willheslam/dynamoose-type-mismatch/
Schema
const schema = new dynamoose.Schema(
{
hashKey: {
type: String,
hashKey: true,
required: true,
},
},
{ saveUnknown: true }
)
Model
const MyModel = dynamoose.model("mytable", schema, modelOptions)
General
await MyModel.batchPut([
{
hashKey: "foo",
bar: null, // comment this out and the type mismatch goes away
},
])
const records = await MyModel.query({ hashKey: { eq: "foo" } }).exec()
Current output and behavior (including stack trace):
(node:25257) UnhandledPromiseRejectionWarning: TypeMismatch: Expected hashKey to be of type string, instead found type object.
at checkTypeFunction (/Users/will.heslam/recreate-dynamoose-bug/node_modules/dynamoose/lib/Document.ts:263:11)
at Array.map (<anonymous>)
at Function.<anonymous> (/Users/will.heslam/recreate-dynamoose-bug/node_modules/dynamoose/lib/Document.ts:282:111)
at Generator.next (<anonymous>)
at /Users/will.heslam/recreate-dynamoose-bug/node_modules/dynamoose/dist/Document.js:8:71
at new Promise (<anonymous>)
at __awaiter (/Users/will.heslam/recreate-dynamoose-bug/node_modules/dynamoose/dist/Document.js:4:12)
at Function.Document.objectFromSchema (/Users/will.heslam/recreate-dynamoose-bug/node_modules/dynamoose/dist/Document.js:192:12)
at Document.<anonymous> (/Users/will.heslam/recreate-dynamoose-bug/node_modules/dynamoose/lib/Document.ts:421:40)
at Generator.next (<anonymous>)
Expected output and behavior:
records [
Document { hashKey: 'foo', foo: null },
lastKey: undefined,
count: 1,
queriedCount: undefined,
timesQueried: 1
]
Environment:
Operating System:
Operating System Version:
Node.js version (node -v
): v12.14.1
NPM version: (npm -v
): 6.13.4
Dynamoose version: 2.1.2
Other information (if applicable):
Other:
- I have read through the Dynamoose documentation before posting this issue
- I have searched through the GitHub issues (including closed issues) and pull requests to ensure this issue has not already been raised before
- I have searched the internet and Stack Overflow to ensure this issue hasn’t been raised or answered before
- I have tested the code provided and am confident it doesn’t work as intended
- I have filled out all fields above
- I am running the latest version of Dynamoose
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (6 by maintainers)
Top GitHub Comments
@willheslam This is actually intentional. It is in preparation for supporting the DynamoDB
null
type in the future.You can find this in the breaking change log:
It is kinda a pain I know. But I felt like this was a worthwhile tradeoff to be able to support the native DynamoDB
null
type in the future.https://github.com/dynamoose/dynamoose/issues/815
Replaced all null / undefined to dynamoose.UNDEFINED, still getting the same error.