Search code examples
javascriptparse-platformparse-cloud-codepfuser

Parse..com cloud code: Trying to update user's column but nothing happens


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


Solution

  • 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.