简体   繁体   English

如何将空字符串作为dynamodb中字段的值传递?

[英]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.我正在尝试使用以下 json 数据进行发布请求。 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'. '一个或多个参数值无效:AttributeValue 不能包含空字符串'。

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 });

  });
}

Yes Dynamodb cannot accepts empty string.是 Dynamodb 不能接受空字符串。 So edit the aws configuration所以编辑aws配置

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

This works!这有效!

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.包含空值的请求将因 ValidationException 异常而被拒绝。

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)

现在支持非键字符串/二进制属性中的空值 - AWS 公告

Amazon DynamoDB 现在支持 DynamoDB 表中非键字符串和二进制属性的空值https://stackoverflow.com/a/61909830/7532347

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM