'DynamoDB Datamapper error throws item did not adhere to the DynamoDBTable protocol
Not really sure where my error is here, Im using the AWS DynamoDb mapper function in node.js but I get this error:
The provided item did not adhere to the DynamoDbTable protocol. No
string property was found at the DynamoDbTable
symbol
I followed the tutorial they provide, I made some changes so I can use cjs but can't seem to find the issue.
const {
DynamoDBSchema,
DynamoDBTable,
embed
} = require("@aws/dynamodb-data-mapper");
const v4 = require("uuid/v4");
class Invoice {}
Object.defineProperties(Invoice.prototype, {
DynamoDBTable: {
value: 'invoices'
},
[DynamoDBSchema]: {
value: {
id: {
type: "String",
keyType: "HASH",
defaultProvier: v4
},
invoiceId: {type:'String'},
firstname:{type:'String'},
lastname:{type:'String'},
email:{type:'String'},
items:{type:'List',memberType:{type:'Map'}},
tax:{type:'String'},
total:{type:'String'},
paymentStatus:{type:'String'},
paymentType:{type:'String'},
paymentInfo:{type:'List',memberType:{type:'Map'}}
}
}
});
module.exports = Invoice;
const AWS = require("aws-sdk");
const {DataMapper} = require('@aws/dynamodb-data-mapper');
const Invoice= require('../models/invoices.model');
const ddb = new AWS.DynamoDB({
region: "us-east-1",
endpoint: "http://localhost:1948"
});
const ddbDoc = new AWS.DynamoDB.DocumentClient({
region: "us-east-1",
endpoint: "http://localhost:1948"
})
const mapper = new DataMapper({
client: new AWS.DynamoDB({region: "us-east-1",
endpoint: "http://localhost:1948"})
})
let data = new Invoice();
//mapper.put({data}).then(console.log).catch(console.log)
async function getItems(){
for await( const foo of mapper.scan(data,{indexName:'Invoiceid-Email'}) ){
console.log(foo)
}
}
getItems().then(console.log).catch(console.log);
Solution 1:[1]
You'll need to put DynamoDBTable
inside square brackets similar to DynamoDBSchema
.
Solution 2:[2]
If you're using Typescript, you have to new
up the model.
@table('myTable')
export class MyModel {
constructor(model?: Partial<MyModel>) {
if (model) {
Object.assign(this, model);
}
}
@hashKey()
pk: string;
name: string;
}
// This won't work
const wontWork: MyModel = { pk: 'Cust#1', name: 'John' };
// You have to do this
const willWork = new MyModel();
willWork.pk = 'Cust#1';
willWork.name = 'John';
// This will also work
const willAlsoWork = new MyModel({ pk: 'Cust#1', name: 'John' });
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Juho Karppinen |
Solution 2 | Kevin Aung |