2015-12-02 70 views
1

我试图通过云代码删除文件。 但此脚本失败:解析云功能失败

Parse.Cloud.define("deleteFile1", function(request, response) { 
    Parse.Cloud.httpRequest({ 
     method: 'DELETE', 
     url: 'https://api.parse.com/1/files/****.png', 
     headers: { 
     "X-Parse-Application-Id": "*******", 
     "X-Parse-REST-API-Key" : "*******" 
     }, 
     success: function(httpResponse) { 
      console.log('Delete succeeded ' + httpResponse.text); 
     response.success(); 
     }, 
     error: function(httpResponse) { 
       response.error("failed"); 
     } 
     }); 
}); 

有错误141,脚本失败。

我这样称呼它:

Map<String, String> map = new HashMap<String, String>(); 
    ParseCloud.callFunctionInBackground("deleteFile1", map, new FunctionCallback<Object>() { 
     @Override 
     public void done(Object object, ParseException e) { 
      if(e == null){ 

      }else{ 
       System.out.println(e.getCode()); 
       utils.toast_error("Couldn't delete image.. try again"); 
      } 

     } 
    }); 

我检查我的主要的,他们是正确的。所以脚本本身在某些方面肯定是错的。我想也许是网址。 /文件假设是图像文件绑定的ParseFile?我试着改变“请求”&“响应”到httpResponse等,但它没有任何区别。

+0

呵呵没问题!是的,我也看到了,主要区别是什么? – Benni

+0

[Heroku](https://www.heroku.com/home)基本上只是Cloud Code的一个更大,更好,功能全面的版本。 (以同样的方式,麦克拉伦P1就像一个更新的,功能齐全的T型福特...) –

回答

1

更新代码现在至少可以正常运行,并且您能够捕获错误。

可以通过修改你的错误处理程序返回的实际消息返回httpResponse.text

error: function(httpResponse) { 
     response.error("failed " + httpResponse.text); 
    } 

起初,我用错了X-Parse-REST-API-Key并得到以下错误:

{ 
    "code": 141, 
    "error": "failed {\"error\":\"unauthorized\"}\n" 
} 

确保使用MASTER键:X-Parse-Master-Key。这固定在我身边的代码,它运行良好。


原始代码失败,因为你的时候,它已经完成了没有你的反应叫success()

以下是调用/deleteFile1时收到的JSON响应;它表明,这就是问题所在:

{ 
    code: 141 
    error: "success/error was not called" 
} 

如果你看Parse Cloud Code docs,你会看到,每个方法使用response(或者在你的代码中,httpResponse)参数,当它已经完成了调用response.success()

总是请阅读Parse发回给您的回复 - 这可能有助于理解错误。


找到相关的帖子,其中建议同样的事情的答案:

+0

请参阅我的编辑,我的Parse.com上的日志信息选项卡现在从脚本记录“失败”。 – Benni