-1

when I am trying to post the data I am getting this error; One or more parameter values were invalid: Missing the key nineId in the item

This is the code.

const express = require('express');
const router = express.Router();
const AWS = require('aws-sdk');
AWS.config.update({
    region:'us-east-2'
})
const dynamodb = new AWS.DynamoDB.DocumentClient();
const dynamodbTableName = 'nine-project';

router.post('/', async(req,res)=>{
    const params = {
        TableName: dynamodbTableName,
        Item: req.body
      }
      await dynamodb.put(params).promise().then(() => {
        const body = {
          Operation: 'SAVE',
          Message: 'SUCCESS',
          Item: req.body
        }
        res.json(body);
      }, error => {
        console.error('Could not decode request: JSON parsing failed', error);
        res.status(500).send(error);
      })
    
})

coder
  • 1
  • 2
  • Please do not post code as screenshots, but use properly formatted code blocks. – Marcin Nov 07 '22 at 03:02
  • Sounds like your table requires a key `nineId` but the request does not contain such a key. Have you tried validating or even debugging the value in `req.body`? – Phil Nov 07 '22 at 03:28
  • check your `body`, pretty sure the pk for dynamodb is different or missing. – Jatin Mehrotra Nov 07 '22 at 03:30

1 Answers1

0

You have to provide the primary key for the table. in this case you missed it that's why you are getting that error

const express = require('express');
const router = express.Router();
const AWS = require('aws-sdk');
AWS.config.update({
    region:'us-east-2'
})
const dynamodb = new AWS.DynamoDB.DocumentClient();
const dynamodbTableName = 'nine-project';

router.post('/', async(req,res)=>{
const body = req.body;
body.nineId = "4a36251d-8668-4335-b746-527ff21a9ece";
// generate uuid for the object
    const params = {
        TableName: dynamodbTableName,
        Item: body
      }
      await dynamodb.put(params).promise().then(() => {
        const body = {
          Operation: 'SAVE',
          Message: 'SUCCESS',
          Item: req.body
        }
        res.json(body);
      }, error => {
        console.error('Could not decode request: JSON parsing failed', error);
        res.status(500).send(error);
      })
    
})