2016-06-28 56 views
1

我有興趣的情況。我有類和表在Azure上:Azure移動應用程序服務異常「該項目不存在」,而InsertAsync

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() 

回答

3

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

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

0

我找到了理由。

對於這個exeption可能是兩個原因

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

  2. Theese例外也扔如果在後端(我的情況下,這是節點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(...); 
}); 

會拋出異常

相關問題