title: Ionic Storage undefined in promise?
body: <p>I have an Ionic 2 app that shows the user Items inside Packages in the form of a list. I created a function that retrieves the current Item index inside a Package, it is fired the user launches the app. It checks if the index name-value pair in Ionic Storage (internal) memory is null. In that case, the index is set to 0 and stored in both internal and working memory. Otherwise, the index is retrieved from internal memory and used to set working memory.</p>
  
  <p>However, <code>self.storage.set(myString, '0');</code> in the code below throws the error <code>TypeError: Cannot call method 'set' of undefined</code>. I checked using <code>console.dir</code> and indeed <code>self.storage</code> was undefined inside the <code>then(...);</code> block. Why is this, and how can I work around it?</p>
  
  <p><b>Imports, declarations, constructor</b></p>
  
  <pre><code>import { Injectable } from '@angular/core';
  
  import { Storage } from '@ionic/storage';
  
  declare var self: ItemStorageService;
  
  @Injectable()
  export class ItemStorageService {
      workingMemoryItems: Array&lt;any&gt;;
      workingMemoryIndex: number;
      packageIndex: number;
  
      constructor(public storage: Storage) {
          self = this;
          self.packageIndex = 0;
  
          self.initializeIndex().then(
              res =&gt; {
                  console.log(res);
              },
              err =&gt; {
                  console.log(err);
              }
          )
      }
  // Other methods including initializeIndex()
  }
  </code></pre>
  
  <p><b>InitializeIndex function</b></p>
  
  <pre><code>public initializeIndex(): Promise&lt;any&gt; {
      console.dir(self.storage); // Returns Storage object
      var myString = 'index_item_package_' + self.packageIndex;
      return new Promise((resolve, reject) =&gt; {
          self.storage.get(myString).then(
              val =&gt; {
                  if ((val === null) || (val === 'null')) {
                      console.dir(self.storage); // Returns undefined
                      self.storage.set(myString, '0');
                      self.workingMemoryIndex = 0;
                      resolve('Working memory index not found and set to 0');
                  } else {
                      self.workingMemoryIndex = val as number;
                      resolve('Working memory index found and set to ' + val);
                  }
              },
              err =&gt; {
                  self.storage.set(myString, 0);
                  self.workingMemoryIndex = 0;
                  reject('Working memory index not found and set to 0');
              }
          )
      });
  }
  </code></pre>