Upserting throws 500 server error

I have a collection balances that i want to update or insert if that record does not exist.

When i run the method like this,

Meteor.methods({
	upsertfun:function(userid){
	//let balance = 0;
        //var balance  = Balances.findOne({userid:userid}).balance;
        //if(balance == null){balance = 0;}
        var paypal_gross = 200;
        //var updatedbalance = parseInt(balance) + parseInt(paypal_gross);
        Balances.update({
                 userid: userid,
                 balance: 30
              }, {
                 $set: {
                    userid: userid,
                    balance: 543
                 }
              }, {
                  upsert: true
              })
	}
});

i am able to upsert but fails when i introduce variables.

This is how it would look with variables in

Meteor.methods({
	upsertfun:function(userid){
	//let balance = 0;
        var balance  = Balances.findOne({userid:userid}).balance;
        if(balance == null){balance = 0;}
        var paypal_gross = 200;
        var updatedbalance = parseInt(balance) + parseInt(paypal_gross);
        Balances.update({
                 userid: userid,
                 balance: balance
              }, {
                 $set: {
                    userid: userid,
                    balance: updatedbalance
                 }
              }, {
                  upsert: true
              })
	}
});

For some reason, this keeps throwing error 500,server error. What can i do to fix this?.

Is your balance variable being assigned properly?

If you console.log(balance) after assignment (ie after the var balance = …) what do you get?

You can see that in the server console. The 500 error is for security reasons, Meteor doesn’t want to tell the client all details. On the server log you can see the full story with line numbers etc.