我正在尝试使用以下json数据执行post请求。但我需要一个字段,即“notes”作为空字符串值传递。当我这样通过时,会有一个错误:
一个或多个参数值无效: AttributeValue可能不包含空字符串。
我怎样才能解决这个问题?
//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 });
});
}
发布于 2019-05-01 22:58:27
是的,Dynamodb不能接受空字符串。所以编辑aws配置
var docClient =新AWS.DynamoDB.DocumentClient({ convertEmptyValues: true });
这行得通!
发布于 2019-05-01 23:00:40
属性及其值的映射。这个映射中的每个条目都由一个属性名和一个属性值组成。属性值不得为null;字符串和二进制类型属性的长度必须大于零;set类型属性不得为空。包含空值的请求将被ValidationException异常拒绝。
您可以通过定义一个函数从对象中删除空字符串来解决这个问题,如
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);
这将从对象中删除空属性。
如果对象包含嵌套对象,则使用以下函数
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)
发布于 2020-05-20 01:44:41
DynamoDB现在支持DynamoDB表https://stackoverflow.com/a/61909830/7532347中的非键字符串和二进制属性的空值。
https://stackoverflow.com/questions/55946990
复制相似问题