2

Here is what my cloud code function looks like:

Parse.Cloud.define("usePromotionCode", function (request, response) {
    Parse.Cloud.useMasterKey();
    var promotionCode = request.params.promotionCode;
    if (promotionCode == null) {
        response.error("Promotion code must be specified!");
    } else {
        var promotionCodeQuery = new Parse.Query("Invite");
        promotionCodeQuery.equalTo("promotionCode", promotionCode);
        promotionCodeQuery.equalTo("used", false);
        promotionCodeQuery.first().then(
            function (invite) {
                if (invite !== undefined) {
                    console.log("Successfully found the invite. Promoting the user");
                    response.success("true");
                } else {
                    console.error("Found invite but it is undefined");
                    response.error("Invalid promotion code");
                }
            },
            function (error) {
                response.error("Invalid promotion code: " + error);
            }
        );
    }
});

And here is the appropriate portion of the log:

E2015-07-09T10:07:13.617Z]v142 Ran cloud function usePromotionCode for user fYHD27Er76 with:
  Input: {"promotionCode":"ThHuf1rhqmfSmfi"}
  Result: Invalid promotion code
I2015-07-09T10:07:13.785Z]Found invite but it is undefined

It appears that the query promise resolves with a success response. However the invite variable that it passes is null as seen from the log. The funny thing is that I can confirm that the object does exists on the server with that promotion code and used=false.
This issue is similar to another stackoverflow question except that my code uses promises and still has issues and is a cloud code function not simply a javascript function.

Is it expected behaviour for a query to resolve a promise successfully and still pass a null/undefined object? If not, what am I doing wrong?
Note that I replaced the first() with a count() and the promise gets resolved successfully with the count being passed with a (non-null) value of 1. Which again confirms that the object does exists on the server!
I also tried the success:, error: callback approach as opposed to Javascript promises, but it doesn't work either.

Community
  • 1
  • 1
reubenjohn
  • 1,351
  • 1
  • 18
  • 43
  • I just tried it, but the issue persists! The else block is still being executed so looks like an `undefined` variable is being passed during the promise resolution. I am updating my question accordingly. – reubenjohn Jul 09 '15 at 10:37
  • Are you getting response from parse by printing `invite`? You have mentioned that` count` returns non-null, so you can use `count` instead of `first` right? – iLaYa ツ Jul 09 '15 at 11:06
  • Technically, yes, but in my case, I need to perform actions on the retrieved invite object, I have removed those actions for test purposes. – reubenjohn Jul 09 '15 at 11:08
  • Is it `undefined` or is it `null`? Your log is different from your code. – Bergi Jul 09 '15 at 20:04
  • It is undefined. Sorry about the mismatch, the code was updated in response to an earlier comment that was deleted. I will update the log now. – reubenjohn Jul 09 '15 at 22:05
  • @reubenjohn do you remember how you solved this? – grantespo May 31 '19 at 07:14

0 Answers0