Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an empty string as value of a field in dynamodb?

I am trying to do a post request with the following json data. But i need one field ie 'notes' to pass as an empty string value. When I am passing like that, an error is getting :

'One or more parameter values were invalid: An AttributeValue may not contain an empty string'.

How can I fix this issue?

//json data which i need to post
  {
  "storeId": "106",
  "addressId": "1",
  "managerId": "1",
  "name": "Syammohan",
  "contactNo": "9656985685",
  "notes": "",
  "bookingType": "Weddding Consult",
  "bookingDate": "2019-05-02",
  "bookingTime": "09:00 am"
}

function bookingDone(employee) {
  var {

    storeId,
    addressId,
    managerId,
    name,
    contactNo,
    notes,
    bookingType,
    bookingStatus,
    bookingTime
  } = req.body

  console.log("notes", notes);

  const params = {
    TableName: "Booking",

    Item: {
      id: id,
      storeId: storeId,
      addressId: addressId,
      managerId: managerId,
      name: name,
      contactNo: contactNo,
      notes: notes,
      bookingType: bookingType,
      bookingStatus: bookingStatus,
      bookingDate: bookingDate,
      bookingTime: bookingTime,
      employeeId: employee.id

    },

  };

  docClient.put(params, (error) => {
    if (error) {
      console.log(error);
      res.status(400).json({ error: 'Could not create booking' });
    }
    // queue.push(JSON.stringify({ event: 'booking.booking.created', model: { 'Bookings': params.Item } }));
    res.send(params.Item)
    // res.json({ id, name, info });

  });
}
like image 634
liza Avatar asked May 02 '19 06:05

liza


2 Answers

Yes Dynamodb cannot accepts empty string. So edit the aws configuration

var docClient = new AWS.DynamoDB.DocumentClient({ convertEmptyValues: true });

This works!

like image 163
liza Avatar answered Sep 20 '22 19:09

liza


A map of attributes and their values. Each entry in this map consists of an attribute name and an attribute value. Attribute values must not be null; string and binary type attributes must have lengths greater than zero; and set type attributes must not be empty. Requests that contain empty values will be rejected with a ValidationException exception.

You can solve the problem by defining a function to remove empty string from the object like

function removeEmptyStringElements(obj) {
  for (var prop in obj) {
    if(obj[prop] === '') {// delete elements that are empty strings
      delete obj[prop];
    }
  }
  return obj;
}
removeEmptyStringElements(req.body);

This will remove the empty attributes from your object.

If your object contains nested object then use the following function

function removeEmptyStringElements(obj) {
  for (var prop in obj) {
    if (typeof obj[prop] === 'object') {// dive deeper in
      removeEmptyStringElements(obj[prop]);
    } else if(obj[prop] === '') {// delete elements that are empty strings
      delete obj[prop];
    }
  }

removeEmptyStringElements(req.body)
like image 22
Nikhil Unni Avatar answered Sep 22 '22 19:09

Nikhil Unni