Reputation: 494
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 });
});
}
Upvotes: 9
Views: 28215
Reputation: 109
Amazon DynamoDB now supports empty values for non-key String and Binary attributes in DynamoDB tables https://stackoverflow.com/a/61909830/7532347
Upvotes: 8
Reputation: 61
Empty values in non-key string/binary attributes is now supported - AWS announcement
Upvotes: 6
Reputation: 494
Yes Dynamodb cannot accepts empty string. So edit the aws configuration
var docClient = new AWS.DynamoDB.DocumentClient({ convertEmptyValues: true });
This works!
Upvotes: 13
Reputation: 779
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)
Upvotes: 7