title: Upserting a record using meteor
body: <p>I have a collection <code>balances</code> that i want to update or insert if that record does not exist. </p>
  
  <p>When i run the method like this,</p>
  
  <pre><code>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
                })
      }
  });
  </code></pre>
  
  <p>i am able to upsert but fails when i introduce variables.</p>
  
  <p>This is how it would look with variables in</p>
  
  <pre><code>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
                })
      }
  });
  </code></pre>
  
  <p>For some reason, this keeps throwing error 500,server error. What can i do to fix this?.</p>