Farhan
Farhan

Reputation: 47

Meteor: Session problems

Im getting this error

TypeError: Cannot read property 'set' of undefined

Code is:

    Router.map(function() {
        this.route('/payment_return/:invoice_no/:amount/', {
            where: 'server',
            onBeforeAction: function() {
                console.log("result");
                result = paypal_return(this.params.invoice_no,this.params.amount,this.params.query.token,this.params.query.PayerID);
                console.log(result);
                if (result)
                {
                    var tokens = this.params.amount*10;
                    console.log(tokens);
                var playerId = this._id;
                Session.set('selectedUser', playerId);
                    var selectedUser = Session.get('selectedUser');
                    Meteor.call('updateTokens', selectedUser, tokens);
                    this.response.end("Payment captured successfully");
                }
                else
                {
                    this.response.end("Error in processing payment");
                }


            }
        });
    });

In, methods.js

Meteor.methods({
    'updateTokens': function(selectedUser, tokens){
        check(selectedUser, String);
        check(tokens, Number);
        var currentUserId = Meteor.userId();
        if(currentUserId){
            Meteor.users.update(selectedUser,
                { $inc: { 'profile.tokens': tokens}});
        }
    }
})

Basically, trying to update user's token amount after successful payment, but unfortunately it's returning just that error.

Upvotes: 0

Views: 422

Answers (3)

Vasil Nedyalkov
Vasil Nedyalkov

Reputation: 65

var paymentRoutes= Picker.filter(function(req, res) {
  return req.method == "GET" || "POST";
});

paymentRoutes.route('/payment_return/:invoice_no/:amount/', function(params, req, res, next) {
      result = paypal_return(params.invoice_no,params.amount,params.query.token, this.userId);
      if (result){
          var tokens = this.params.amount*10;
          var playerId = this.userId;
          Meteor.users.update({_id:playerId},{ $inc: { 'profile.tokens': tokens}});
          res.end("Payment captured successfully");
      }else{
          res.end("Error in processing payment");
      }
});

I hope this will be helpful, Cheers

Upvotes: 1

Vasil Nedyalkov
Vasil Nedyalkov

Reputation: 65

This looks like API call to me, so I will suggest you to use meteorhacks:picker

Then you can add on your server side:

    var paymentRoutes= Picker.filter(function(req, res) {
  return req.method == "POST"; //OR GET WHATEVER YOU NEED
});

    paymentRoutes.route('/payment_return/:invoice_no/:amount/', 
    function(params, req, res, next) {
      //UPDATE TOKEN
    });

Upvotes: 1

Vasil Nedyalkov
Vasil Nedyalkov

Reputation: 65

Sessions are only available in client side... Not sure where you are trying to call Session, but if Session package is included and you are calling Sessions.set/get on client it should work.

Upvotes: 1

Related Questions