0

I just want to be able to "Modify" the 'content' variable from inside the callback function.

class MyClass extends React.Component {
  render() {
  var content = 'initial value';

  const token = this.props.match.params.token;
  Meteor.call('checkResetToken', token, (error, result) => {
    console.log('content: ', content); // prints 'initial value'
    if (result) {
      content = 'true result';
    } else {
      content = 'false result';
    }

    console.log('content inside the callback: ', content); // 'false result'
  });

  return 'content: ' + content; // prints 'initial value'
  }
}

It sounds like the callback takes a local copy of the content variable, but how can I change the variable ITSELF.

Any help would be highly appreciated

Hend El-Sahli
  • 6,268
  • 2
  • 25
  • 42
  • 4
    It's because `Meteor.call` is **asynchronous**. Your `return` statement runs _before_ the callback in the ".call" executes. So the callback changes the value correctly, but you're not waiting for it before you try to read the value. – ADyson Mar 20 '18 at 11:48
  • @ ADyson Thank you very mush for your help – Hend El-Sahli Mar 20 '18 at 11:56

0 Answers0