Search code examples
c#azurewindows-phone-8azure-mobile-services

DeleteAsync MobileServices Timed out


I have the following code.

The C# code is on the device, Windows Phone 8:

IMobileServiceTable<Subscription> SubsciptionsTable = App.MobileService.GetTable<Subscription>();
SubscriptionItemServerItem = await SubsciptionsTable.Where(subs => subs.userId == App.UserInfromationID && subs.ContentID == holdElement.NewMessages).ToListAsync();//only want items that are something
await SubsciptionsTable.DeleteAsync(SubscriptionItemServerItem[0]);

Where I send the delete request to the Azure MobileService. Here I have the following Script:

    function del(id, user, request) {
    var TableA_Table = tables.getTable('subscription');

    //console.log("About to Delete Subscription:", id);
    TableA_Table.where({ userId: user.userId, id: id})
        .read ({ success: DeleteItem });

    function DeleteItem(results)
    {
        if(results > 0){
            console.log("Reached HERE", id);
            request.execute();
        }
    }
    }

I am using this script to verify the user is allowed to delete what he is requesting, i.e. That the userdeleting has the userId matching the userId in the Table.

Everything works and I reach the

    request.execute(); 

But then it hangs and my await on the phone, times out. And the item is not deleted on the server either.

What Should I do? And what could cause the error?

Solution

I still did not find the error in my script. But by tweaking the response I got, that I marked as answered I found the solution to be:

function del(id, user, request) 
{
  var table = tables.getTable('subscription');

  table.where({ id: id }).read({
    success: function (results) 
    {
      if (results.length > 0) 
      {
        //console.log("log existingItem",results[0].userId);
        //console.log("log user",user.userId);
        if (results[0].userId == user.userId) 
        {
          request.execute();
        } 
        else 
        {
          // console.log("Invalid user");
          request.respond(statusCodes.BAD_REQUEST, "Invalid user");
        }
      } 
      else 
      {
        // console.log("NOT_FOUND");
        request.respond(statusCodes.NOT_FOUND);
      }
    }, error: function () {
      //console.log("NOT_FOUND_error");
      request.respond(statusCodes.NOT_FOUND);
    }
  });
}

Solution

  • Try to update your "delete" function to this:

    function delete(item, user, request) 
    {
        var table = tables.getTable('subscription');
        table.where({ id: item.id }).read({
        success: function (results) 
        {
            if (results.length) 
            {
                var existingItem = results[0];
                if (existingItem.UserId === user.userId) 
                {
                    request.execute();
                } 
                else 
                {
                    request.respond(statusCodes.BAD_REQUEST, "Invalid user");
                }
            } 
            else 
            {
                request.respond(statusCodes.NOT_FOUND);
            }
        }, error: function () {
        request.respond(statusCodes.NOT_FOUND);
        }
        });
    }