lito
lito

Reputation: 3125

why Parse.Query is always success even when is not?

In a parse.com Cloud Code I have a very simple function "IsNameAlreadyTaken" but no matter what "username" I pass, the "success" is always executed. Why is that?

Parse.Cloud.define("IsNameAlreadyTaken", function(request, response) {
    Parse.Cloud.useMasterKey();
    var query = new Parse.Query(Parse.User);
    console.error("Please log request.params.username: " + request.params.username + "<++++");
    query.equalTo("username", request.params.username);
    query.first({
        success: function(user) {
            response.success("user was found." + user);
        },
        error: function(error) {
            response.error("Could not find user." + error);
        }
    });
});

Here I'm testing with the username already taken:

curl -X POST \
-H "X-Parse-Application-Id: uOXz....rPt" \
-H "X-Parse-REST-API-Key: GAv...SmK" \
-H "Content-Type: application/json" \
-d '{"username": "UsernameAlreadyTaken"}' \
https://api.parse.com/1/functions/IsNameAlreadyTaken

and the respond is fine:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100       34 --:--:-- --:--:-- --:--:--    68{"result":"user was found. [object Object]"}

The problem is that the exact behavior is when a username DOES NOT EXIST:

curl -X POST \
-H "X-Parse-Application-Id: uOXz....rPt" \
-H "X-Parse-REST-API-Key: GAv...SmK" \
-H "Content-Type: application/json" \
-d '{"username": "nonExistedUsername"}' \
https://api.parse.com/1/functions/IsNameAlreadyTaken

and the respond is the same: WTF!!!

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  100         61 --:--:-- --:--:-- --:--:--   105{"result":"user was found. undefined"}

Any idea what is going on? is this a bug in Parse.com?

Upvotes: 0

Views: 28

Answers (1)

MartyIX
MartyIX

Reputation: 28648

Wild guess: I think that when user does not exist, you get user === undefined information (if it exists, you get the user object), so you have the information you need.

error: callback may be called only when connection fails or something like that. That's kind of standard handling in many cases in JavaScript.

Upvotes: 1

Related Questions