简体   繁体   English

通过 .NET api 调用时,DynamoDB UpdateItem 不工作

[英]DynamoDB UpdateItem not working when called via .NET api

I am using DynamoDb to set up a table which can be updated via API calls.我正在使用 DynamoDb 设置一个可以通过 API 调用更新的表。 After going through DynamoDB documentation, I have written an update item function in .NET as follows:翻阅DynamoDB文档后,我在.NET中写了一个更新项function如下:

public static async Task IntAsync(string phone, string s)
    {
        getAsync(phone).Wait();
        AmazonDynamoDBConfig clientConfig = new AmazonDynamoDBConfig();
        clientConfig.RegionEndpoint = RegionEndpoint.APSouth1;
        try
        {
            using (IAmazonDynamoDB ddbClient = new AmazonDynamoDBClient(clientConfig))
            {
                await ddbClient.UpdateItemAsync(new UpdateItemRequest
                {
                    TableName = "TableName",
                    Key = new Dictionary<string, AttributeValue>
                    {
                        {"ID", new AttributeValue{S=phone} },
                    },
                    ExpressionAttributeNames = new Dictionary<string, string>()
                    {
                        {"#P", "Progress" },
                        {"#A", "attempt"+count.ToString() },
                        {"#S", s }
                    },
                    ExpressionAttributeValues = new Dictionary<string, AttributeValue>()
                    {
                        {":s",new AttributeValue {S=DateTime.Now.ToString()}},

                    },
                    UpdateExpression = "SET #P.#A.#S = :s",
                    ConditionExpression = "attribute_not_exists(#P.#A.#S)"
                });
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Update Error: " + e.Message);
        }
    }

Issue: This function works fine when called via main and the changes are reflected instantaneously, however calling the same function via a.Net core Get api request does not return any result and continues saying sending request on postman.问题:当通过 main 调用时,此 function 工作正常并且更改会立即反映出来,但是通过 a.Net core 调用相同的 function Get api 请求不会返回任何结果,并继续说在 postman 上发送请求。

The Get Request is as follows:获取请求如下:

 public async Task<string> GetAsync(string mobile, string s)
 { 
    await Program.IntAsync(string mobile, string s); 
    return "Done";
 }

However removing the function call from the get request results in the request going through and all the other functions are working perfectly.但是,从 get 请求中删除 function 调用会导致请求通过,并且所有其他功能都可以正常工作。 I have several other update requests carried out via get requests, all of them work perfectly.我还有其他几个通过 get 请求执行的更新请求,它们都运行良好。

Here is what postman keeps saying这是 postman 一直在说的话在此处输入图像描述

Welcome to the community BYNARI欢迎来到 BYNARI 社区
Your code looks good, except for not returning any value, I would change it to have an update response as UpdateItemAsync returns a response:) Change this您的代码看起来不错,除了不返回任何值外,我会将其更改为具有更新响应,因为 UpdateItemAsync 返回响应:)更改此

public static async Task IntAsync(string phone, string s)
{
    //getAsync(phone).Wait();
    AmazonDynamoDBConfig clientConfig = new AmazonDynamoDBConfig();
    clientConfig.RegionEndpoint = RegionEndpoint.APSouth1;
    try
    {
        using (IAmazonDynamoDB ddbClient = new AmazonDynamoDBClient(clientConfig))
        {
            await ddbClient.UpdateItemAsync(new UpdateItemRequest

To

public static async Task IntAsync(string phone, string s)
    {

        //Added this variable
        UpdateItemResponse updateResponse = null;
        getAsync(phone).Wait();
        AmazonDynamoDBConfig clientConfig = new AmazonDynamoDBConfig();
        clientConfig.RegionEndpoint = RegionEndpoint.APSouth1;
        try
        {
            using (IAmazonDynamoDB ddbClient = new AmazonDynamoDBClient(clientConfig))
            {
              //using the variable
                updateResponse = await ddbClient.UpdateItemAsync(new UpdateItemRequest
***

At the end you should return the updateResponse if you would not do any transforming to it最后,如果您不对其进行任何转换,则应返回 updateResponse

and you'd of course check if not null then return your updateResponse to your "GetAsync" method你当然会检查如果不是 null 然后将你的 updateResponse 返回给你的“GetAsync”方法
Even in your GetAsync method you'd want to return your UpdateItemResponse即使在您的 GetAsync 方法中,您也想返回 UpdateItemResponse

so it would look something like:所以它看起来像:

 public async Task<string> GetAsync(string mobile, string s)
  { 
     var response = await IntAsync(string mobile, string s); 
     //Then you pack/unpack your response to get your attributes and
     //convert them to string if that is what you need
     var valueToReturn = CustomStringMethod(response.Attributes);
     return valueToReturn;
  }

Here is a link to the aws developer guide that might help you as well:) Update a record in DynamoDB这是 aws 开发人员指南的链接,它也可能对您有所帮助:) 更新 DynamoDB 中的记录

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

相关问题 updateItem/putItem object 到 dynamodb 中的数组 - updateItem/putItem object into array in dynamodb 如何通过 API 网关从 Jupyter Notebook 更新 DynamoDB? - How do you update DynamoDB from Jupyter Notebook via API Gateway? Dynamodb.put 在运行 lambda 测试配置时工作正常,但在通过 API 网关调用 lambda 时却不行 - Dynamodb.put works fine when running a lambda test configuration, but not when invoking the lambda through API gateway 使用 C# .NET Core 2.1 查询 DynamoDB GSI 时出错 - Error when Querying DynamoDB GSI Using C# .NET Core 2.1 使用 AWS API 网关和 DynamoDB 的自定义 ID - Custom IDs with AWS API Gateway and DynamoDB AWS dynamodb ScanInput ExpressionAttributeValue 未按预期工作 - AWS dynamodb ScanInput ExpressionAttributeValue not working as expected Dialogflow - 通过 API 检测意图不适用于知识库意图 - Dialogflow - Detect intents via API is not working for knowledge base intents DynamoDB 分页 - 知道什么时候没有更多结果 - DynamoDB pagination - knowing when there is no more results REST API - 在 dynamoDB 中检索以前的查询 - REST API - Retrieve previous query in dynamoDB 嵌套字段不起作用的 DynamoDB 流过滤器 - DynamoDB streams filter with nested fields not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM