1

i have a background job that is trying to update user balance with a new balance if they won the league

This functions are helper functions which are being called properly but "query.first" in updateUserEarnings function and "then" function inside updateWinningsHelper are never called, at least nothing is shown in log, . And yes I am user masterkey.

var updateUserEarnings = function (User,prizeValue){
    //var user = new Parse.User();
    console.log("updateUserEarnings Called");
    console.log("User Id: " + User.id);//Shown in log fi
    console.log("PrizeValue " + prizeValue);//Shown in log file
    var query = new Parse.Query(Parse.User);
    query.equalTo("objectId",User.id);
    return query.first({
        success: function(result){
            console.log("updateUserEarnings success");//Never Reached here
            var balance;
            if (result.get("cashBalance")){
                balance = result.get("cashBalance");
            }else{
                balance = 0;
            }
            console.log("Current Balance "+ balance + " for "+result.get("DisplayUsername"));
            balance = balance + prizeValue;
            result.set("cashBalance",balance);

            console.log("User " + result.get("DisplayUsername") + " Balance Updated : " + balance);
            return result
        },error:function(error){
            console.log("user update error "+error);//never reached here as well
            return error;
        }
    })
}
var updateWinningsHelper = function(index,lastIndex,record){
    var winningObj = record.winningObject;
    console.log("Winning object "+ JSON.stringify(winningObj));//Shown in log
    console.log("Index : "+index + " lastIndex : "+lastIndex);//Shown in log
    if (index < lastIndex){
        updateUserEarnings(winningObj.get("User"), winningObj.get("League").get("PrizeValue")).then(
            function(user){
                console.log("Inside then function");//Never Reached
                saveAllObjects.push(user);
            },
            function(error){
                console.log("Error Happened "+error );//never reached
            }
        );
        winningObj.set("LeagueWon",true);
        winningObj.set("isRedeemed",true);

    }else{
        winningObj.set("LeagueWon",false);
    }
    index++;
    winningObj.set("Rank",index);

    return  winningObj;
};

Any help is appreciated

Asif Alamgir
  • 1,454
  • 1
  • 16
  • 41

2 Answers2

1

Turns out you don't need to query again if you already have User pointer from main query. I included User column with the main query and I can use regular set function to that user.

all i did was

var updateUserEarnings = function (User,prizeValue){
//var user = new Parse.User();
User.set("cashBalance", User.get("cashBalance")+prizeValue);

}

hope this helps anyone in the future and not waste 5 hours like me.

Asif Alamgir
  • 1,454
  • 1
  • 16
  • 41
1

Glad you found the problem!

While on the topic, you may want to look into removing any confusion caused by mixing javascript programming paradigms. You use the old callback paradigm as well as the newer Promises for asynchronous operations, which is a mess to debug.

Parse Engineering on JS Promises

I looked at your code for a fair bit of time before realizing how you were trying to perform the query's old-style callback, then return a promise from the query to the calling function, and then use the promise callback in the update helper.

Russell
  • 3,099
  • 2
  • 14
  • 18