[Solved]Assigning value to a variable in a Meteor Call Callback


#1

Hi All,

I am trying to set the value of a variable in a Meteor call callback. each time when it try i get

Exception in delivering result of invoking 'Transaction': TypeError: Cannot set property 'killjoy' of undefined

Transaction -Meteor Method
Killjoy is the variable.

This is how my code looks like

constructor(props){
    super(props);
    this.state={
      killjoy:""
    };
  }
Meteor.call('Transaction', {}, function(err, response) {
      if (err) {
        // handle error
      } else {
        //console.log(response);
        this.state.killjoy = response;
        //console.log(variable);
      }

    });

I am expecting a response of the meteor call to be available globally for other function to follow.
can this be done ?


#2

You should change the callback to an arrow function to get the behavior I’m guessing you want. Otherwise “this” will just be the window.


#3

#4

Thank you it did the trick