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.