0

I am trying to create a record in dynamodb(Using dynamoose). code is

class Test {
  constructor() {
    this.table = dynamoose.model(tableName, tableSchema);
  }
//  userdata object - { 
//  cusotmerEmail: '[email protected]',
//  customerBusinessName: 'DoogleDnd',
//  customerFirstName: 'Tushar',
//  customerId: 101211,
//  customerLastName: 'Gaurav',
//  isDeleted: false,
//  sku: '100',
//  userId: '5c1776e94bea867c3f896236' 
// }


  async createUser(userData) {
    try {
      const res = await this.table.create(userData);
      console.log('Update user record - ', res);
      return res;
    } catch (error) {
      throw new Error(error);
    }
  }
}

*input values to the create function are correct as the same input I tried with batchPut(), it's working. And even update call to the table is also working.

async updateUser(userData) {
    try {
      const res = await this.table.update(userData);
      console.log('Updated user record - ', res);
      return res;
    } catch (error) {
      throw new Error(error);
    }
 }

This is the error I am getting - Error - {"message":"The conditional request failed", "code":"ConditionalCheckFailedException", "statusCode":400}

This is the calling function -

module.exports.subscribeUser = async (event) => {
  let inputBody = (typeof event.body === 'object' ? event.body : 
  JSON.parse(event.body));
  inputBody.userId = event.pathParameters.id;
  try {
    // Validate input
    await asisvc.validateInput(inputBody);

    inputBody = await UserSvc.constructUserObject(inputBody);
    console.log('Constructed object - ', JSON.stringify(inputBody));

    const userData = await testObj.createUser(inputBody);
    return Utils.buildResp(codes.ok, { userData }, {});
  } catch (error) {
    console.log(error);
    return Utils.buildResp(codes.badrequest, { Error: 
Utils.getErrString(error) }, {});

} };

I tried googling it, but didn't find any proper document. Thanks in advance.

4
  • Can you also show the code where you call these functions and write what is the expected output and what actually happens? Commented Dec 17, 2018 at 16:50
  • 3
    Possibly unrelated, but you do know that a) there's no benefit to catching and re-throwing your error if you don't actually do anything else with it, and b) the Error constructor takes a string message as its first parameter, not an object. Commented Dec 17, 2018 at 16:55
  • Also, what actual error did you get? Commented Dec 17, 2018 at 16:57
  • @libik Edited the question, Please have a look. Commented Dec 17, 2018 at 20:11

1 Answer 1

1

In Dynamoose by default we check to see if the primary key already exists in the table when using the Model.create method.

So your error:

{"message":"The conditional request failed", "code":"ConditionalCheckFailedException", "statusCode":400}

Indicates that the primary key already exists in the table. So you are trying to create a duplicate item.

In the documentation there is an options property that you can use to allow overriding the object.

For example the following code will allow overrides:

const res = await this.table.create(userData, {overwrite: true});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.