title: Javascript/Typescript 'this' scope
body: <p>I am working with Ionic2 and Meteor. I do however have a Javascript/Typescript issue relating to the scope of the <code>this</code> object.</p>
  
  <p>I have <a href="https://stackoverflow.com/questions/19277395/why-does-the-value-of-this-changes">read</a> that I should use <code>bind</code> when I don't have handle on <code>this</code> at the appropriate level.</p>
  
  <p>I probably don't understand the concept, because I try the following, but get an error trying to call a function.</p>
  
  <pre><code>this.subscribe('messages', this.activeChat._id, this.senderId, () =&gt; {
      this.autorun(() =&gt; {
          let promiseMessages: Promise&lt;Mongo.Collection&lt;Message&gt;&gt; = this.findMessages();
          promiseMessages.then((messageData: Mongo.Collection&lt;Message&gt;) =&gt; {
          messageData.find().forEach(function (message: Message) {
              setLocalMessage.bind(message);
          });
      });
  });
  </code></pre>
  
  <p>and </p>
  
  <pre><code>private setLocalMessage(message: Message): void {
     this.localMessageCollection.insert(message);
  }
  </code></pre>
  
  <p>I get the following error when I try build the app:</p>
  
  <blockquote>
  <pre><code>ERROR in ./app/pages/messages/messages.ts
  (72,19): error TS2304: Cannot find name 'setLocalMessage'.
  </code></pre>
  </blockquote>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Thank you for the advise below.</p>
  
  <p>I am now using the following, and it works.</p>
  
  <pre><code>          let promiseMessages: Promise&lt;Mongo.Collection&lt;Message&gt;&gt; = this.findMessages();
            promiseMessages.then((messageData: Mongo.Collection&lt;Message&gt;) =&gt; {
              messageData.find().forEach((message: Message) =&gt; {
                this.setLocalMessage(message);
              });
            });
  </code></pre>