简体   繁体   中英

How to handle error object from parse.com cloud callback?

I have a cloud function

Parse.Cloud.define("register", function (request, response) {
    var params = request.params;

    var pass1 = params.pass1;
    var pass2 = params.pass2;

    if (pass1.length < 8 || pass2.length < 8) {
        response.error("Your password is too short!");
        return;
    }
});

and calling this cloud function with javascript:

function register(){
   Parse.Cloud.run("register", { pass1 : "abc", pass2 : "abc", {
      error: function(error){
          alert("Error! --> error msg: " + error.message); 
      },
      success: function(){
          alert("Success !");
      }
   });
}

The problem is I cannot read the error message! I'm getting undefined object.

Error! --> error msg: undefined

When you use 'response.error("")' to return an error value, it will return the following response:

{"code":141,"error":"Your password is too short!"}

So you should get error.error instead of error.message.

function register(){
   Parse.Cloud.run("register", { pass1 : "abc", pass2 : "abc", {
      error: function(error){
          alert("Error! --> error msg: " + error.error); 
      },
      success: function(){
          alert("Success !");
      }
   });
}

error.message didn't work for me one time, but it was just a bug in my code. Later error.message worked as expected. https://www.parse.com/questions/parsepromiseerror-not-printing-the-error-message-or-code

If you use: response.error("Your password is too short!"); Then you should expect: alert("Error! --> error msg: " + error.message);

Note: For your particular issue, your cloud code might not be reaching the if statement.

I found when calling response.error() from a beforeSave Cloud Code function, the returned error was actually an array. So to access the error message, use

error[0].message

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM