Azure移动应用服务异常"项目不存在"而InsertAsync

本文关键字:quot 不存在 InsertAsync 项目 移动 应用服务 Azure 异常 | 更新日期: 2023-09-27 17:52:52

我有兴趣的情况。我在Azure上有类和表:

public class InmeItem
{
      public string Id { get; set; }
      [JsonProperty(PropertyName = "heartrate")]
      public string Heartrate { get; set; }
      [JsonProperty(PropertyName = "pulsewave")]
      public string Pulsewave { get; set; }
}

我有下面的代码插入新的项目到表:

public static async Task InsertInmeItem(InmeItem inmeitem)
{
     try
     {
         await App.MobileService.GetTable<InmeItem>().InsertAsync(inmeitem);
     }
     catch (Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException ex)
     {
           Debug.WriteLine("This is f***** situation which post data but generate exception: " + ex.ToString());
     }
     catch (Exception ex)
     {
           Debug.WriteLine(ex);
     }
}

但是我有一些感兴趣的情况-运行抛出异常"项目不存在",但数据被插入到Azure上的表中,没有任何异常

异常信息:

This is f***** situation which post data  but generate exception: Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: The item does not exist
   at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.<ThrowInvalidResponse>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.<SendRequestAsync>d__1d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.<RequestAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.WindowsAzure.MobileServices.MobileServiceTable.<>c__DisplayClass14.<<InsertAsync>b__13>d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.WindowsAzure.MobileServices.MobileServiceTable.<TransformHttpException>d__4d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.WindowsAzure.MobileServices.MobileServiceTable.<InsertAsync>d__1a.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.WindowsAzure.MobileServices.MobileServiceTable`1.<TransformHttpException>d__41.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.WindowsAzure.MobileServices.MobileServiceTable`1.<InsertAsync>d__b.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.WindowsAzure.MobileServices.MobileServiceTable`1.<InsertAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at InmeTesting.Models.Backoffice.<InsertInmeItem>d__2.MoveNext()

Azure移动应用服务异常"项目不存在"而InsertAsync

您需要确保从插入函数返回正确的结果,即它期望您返回插入的项。你不一定需要返回插入的项目,但你必须返回一些东西来呈现给客户端,否则框架将返回404,而不是消息"该项目不存在"。

第一个例子成功是因为context。Execute返回插入的项。在第二个示例中,项目显然没有从.then()调用中的代码块返回。

我找到原因了。

对于这个例外可能有两个原因

  1. 不等于数据库的类名和云上的表名(但是类是相等的)

  2. 这些异常也抛出如果后端(在我的情况下,这是Node JS)在table.insert(function (context) {...});then()return context.execute();

例如下面的代码

table.insert(function (context) {
   //context.item.userId = context.user.id;
   //Retranslate data to inme server
   retranslateToInme(context.item.heartrate, context.item.pulsewave);
   return context.execute();
});

不抛出异常。但是:

table.insert(function (context) {
   //context.item.userId = context.user.id;
   //Retranslate data to inme server
   retranslateToInme(context.item.heartrate, context.item.pulsewave);
   return context.execute().then(...);
});

将抛出异常