为什么 Amazon DynamoDB 的 Delete 方法没有 return 删除结果?

Why does Delete method of Amazon DynamoDB not return deleted result?

我正在为 删除操作 创建一个快递 api,成功删除项目后,我希望删除的项目显示为响应,该项目正在从 table 中删除,但 API 给出 空对象 {} 作为响应,当 运行 来自 CURL 命令时,我正在关注这些 docs from aws.

这是我的代码

router.delete('/delete/:id',function(req, res, next) {
  
  const params = {
    TableName: 'Notes',
    Key: {
      id: req.params.id,
    },
  };

  dynamoDb.delete(params, function (error,data) {
    if (error) {
      console.log(error);
      res.status(400).json({ error: 'Could not update user' });
    }
    res.status(200).json(data);
  });
})

curl 命令

curl -H "Content-Type: application/json" -X DELETE xxxxxxxxxxxxxxx/dev/user/delete/alexdebrie2 

DeleteItem操作的DynamoDB documentation明确说明:

In addition to deleting an item, you can also return the item's attribute values in the same operation, using the ReturnValues parameter.

因此默认情况下,删除操作不会 return 已删除项目的旧值。如果需要,您需要将 ReturnValues 选项传递给 DynamoDB 请求。我不熟悉你用来实现这个的任何编程语言,但我希望你能自己解决这个问题。

let params = {
    TableName: tablename,
    Key: {
      hashKey: ,`enter code here`,
      id: `enter code here`
    },
    ConditionExpression: "attribute_exists(hashKey)",// checks if item exists
    ReturnValues: 'ALL_OLD'
  };
  dynamoDB.delete(params, (error, result) => {
    if (error) {
      console.error(error);
      callback(null, {
        statusCode: error.statusCode,
        body: JSON.stringify(error),
      });
      return;
    }
    else {
      console.log(result);
    }
    // create a response
    const response = {
      isBase64Encoded: false,
      statusCode: 200,
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(result),
    };
    console.log(response);
    callback(null, response);
  });
})