簡體   English   中英

InsertAsync時,Azure移動應用程序服務異常“該項目不存在”

[英]Azure Mobile App service exception “The item does not exist” while InsertAsync

我有興趣的情況。 我在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()

您需要確保從insert函數返回正確的結果,即它希望您返回插入的項目。 您不一定需要返回插入的項目,但必須返回要呈現給客戶端的內容,否則框架將返回404而不是“項目不存在”的消息。

第一個示例成功,因為context.execute返回插入的項目。 在第二個示例中,顯然沒有從.then()調用中的代碼塊返回該項。

我找到了理由。

對於此例,可能有兩個原因

  1. 不等於數據庫類的名稱和雲上表的名稱(但類相等)

  2. 如果在table.insert(function (context) {...});后端(在我的情況下,這是Node JS),Theese異常也會拋出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(...);
});

將拋出異常

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM