title: How to process the return from sqlite Select statement in ionic 2?
body: <p>I'm working on a Ionic 2 (<code>2.0.0-rc.2</code>) app with a sqlite storage and i've ran into some problems, hence i'm seeking for help here.</p>
  
  <p>When starting the app for the first time i'm creating the database(tables) insert a few values. So far everything goes alright. However when I try to retrieve data and read it from the database i'm getting an error.</p>
  
  <p><b>The class were i'm talking about:</b></p>
  
  
  
  <pre class="lang-js prettyprint-override"><code>import {Component} from '@angular/core';
  import {Platform} from 'ionic-angular';
  import {StatusBar, Splashscreen, SQLite} from 'ionic-native';
  import {HomePage} from '../pages/home/home';
  
  
  @Component({
      template: `&lt;ion-nav [root]="rootPage"&gt;&lt;/ion-nav&gt;`
  })
  export class MyApp {
      users = [];
      rootPage = HomePage;
  
      constructor(platform: Platform) {
          platform.ready().then(() =&gt; {
              StatusBar.styleDefault();
              let db = new SQLite();
              console.log("open database app.compontent")
              db.openDatabase({
                  name: "data.db",
                  location: "default"
              }).then(() =&gt; {
                  db.executeSql("CREATE TABLE IF NOT EXISTS user (iduser INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT, castleType TEXT, castleHealth INTEGER); ", {}).then((data) =&gt; {
                      console.log("USER TABLE CREATED: ", data);
                  }, (error) =&gt; {
                      console.error("USER Unable to execute sql", error);
                  });
                  db.executeSql("CREATE TABLE IF NOT EXISTS melding (idmelding INTEGER PRIMARY KEY AUTOINCREMENT, meldingType TEXT, meldingTitel TEXT, meldingOmschrijving TEXT, ruimte INTEGER, user_iduser INTEGER, FOREIGN KEY (user_iduser) REFERENCES user(iduser)); ", {}).then((data) =&gt; {
                      console.log("MELDING TABLE CREATED: ", data);
                  }, (error) =&gt; {
                      console.error("MELDING Unable to execute sql", error);
                  });
                  db.executeSql("CREATE TABLE IF NOT EXISTS kasteelOnderdelen (idkasteelOnderdelen INTEGER PRIMARY KEY AUTOINCREMENT, onderdeelNaam TEXT, onderdeelShort TEXT, onderdeelHealth TEXT); ", {}).then((data) =&gt; {
                      console.log("KASTEELONDERDELEN TABLE CREATED: ", data);
                  }, (error) =&gt; {
                      console.error("KASTEELONDERDELEN Unable to execute sql", error);
                  });
                  db.executeSql("CREATE TABLE IF NOT EXISTS user_has_kasteelOnderdelen (id INTEGER PRIMARY KEY AUTOINCREMENT, user_iduser INTEGER, kasteelOnderdelen_idkasteelOnderdelen INTEGER, FOREIGN KEY (user_iduser) REFERENCES user(iduser), FOREIGN KEY (kasteelOnderdelen_idkasteelOnderdelen) REFERENCES kasteelOnderdelen(idkasteelOnderdelen));", {}).then((data) =&gt; {
                      console.log("USER_HAS_KASTEELONDERDELEN TABLES CREATED: ", data);
                  }, (error) =&gt; {
                      console.error("USER_HAS_KASTEELONDERDELEN Unable to execute sql", error);
                  });
                  console.log('Creating database complete!');
                  db.executeSql("INSERT INTO user (firstname, lastname, castletype, castleHealth) VALUES (?, ?, ?, ?)", ['Jan', 'Klaassen', 'mannelijk', '10']).then((data) =&gt; {
  
                      console.log("Inserted row 1: ", data);
                  }, (error) =&gt; {
                      console.error("Unable to insert row 1: ", error);
                  });
                  db.executeSql("INSERT INTO user (firstname, lastname, castletype, castleHealth) VALUES (?, ?, ?, ?)", ['Klaas', 'Janssen', 'mannelijk', '5']).then((data) =&gt; {
  
                      console.log("Inserted row 2: ", data);
                  }, (error) =&gt; {
                      console.error("Unable to insert row 2: ", error);
                  });
                  db.executeSql('SELECT * FROM user', []).then((resultSet) =&gt; {
                      if(resultSet.res.rows.length &gt; 0) {
                          this.users = [];
                          for(let i = 0; i &lt; resultSet.res.rows.length; i++) {
                              this.users.push({
                                  "iduser": resultSet.res.rows.item(i).iduser,
                                  "firstname": resultSet.res.rows.item(i).firstname,
                                  "lastname": resultSet.res.rows.item(i).lastname,
                              });
                          }
                      }
                  }, (error) =&gt; {
                      console.log(error);
                  });
  
              }, (error) =&gt; {
  
                  console.error("Unable to open database", error);
              });
              Splashscreen.hide();
          });
      }
  }
  </code></pre>
  
  <p><b>error (when I run it on a device (Nexus 5):</b></p>
  
  <pre><code>D/SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 12 : Error: Uncaught (in promise): TypeError: Cannot read property 'rows' of undefined
                                                                                           at s (file:///android_asset/www/build/polyfills.js:3:8568)
                                                                                           at file:///android_asset/www/build/polyfills.js:3:8940
                                                                                           at t.invokeTask (file:///android_asset/www/build/polyfills.js:3:14051)
                                                                                           at Object.inner.inner.fork.onInvokeTask (file:///android_asset/www/build/main.js:3:22982)
                                                                                           at t.invokeTask (file:///android_asset/www/build/polyfills.js:3:13987)
                                                                                           at e.runTask (file:///android_asset/www/build/polyfills.js:3:11411)
                                                                                           at i (file:///android_asset/www/build/polyfills.js:3:8028)
                                                                                           at u (file:///android_asset/www/build/polyfills.js:2:16275)
                                                                                           at file:///android_asset/www/build/polyfills.js:2:16399
                                                                                               at MutationObserver.r (file:///android_asset/www/build/polyfills.js:2:14841)
  </code></pre>
  
  <p><b>Additional findings/notes:</b></p>
  
  <p>If i write <code>console.log(resultSet);</code>
  The return will be <code>[object object]</code>
  For now it's all in the same class and on the longrun it will be in it's own StorageProvider/Service but i've moved it all here to debug it. (without success so far)</p>
  
  <p>Does someone have a clue what causes the error?</p>
  
  <p>Thanks in advance.</p>
  
  <p>Poerkie</p>
  
  <p><b>UPDATE 20-12 (after implementing tips and suggestions):</b></p>
  
  <p>current code:</p>
  
  <pre><code>import {Injectable} from '@angular/core';
  import 'rxjs/add/operator/map';
  import {SQLite} from 'ionic-native';
  import {Platform} from 'ionic-angular';
  
  
  @Injectable()
  export class StorageService {
  public database: SQLite;
  public users;
  
  constructor(private platform: Platform) {
      console.log(' StorageService Initialised');
      this.platform.ready().then(() =&gt; {
          this.database = new SQLite();
          this.database.openDatabase({name: "data.db", location: "default"}).then(() =&gt; {
              console.log("SUCCES Opening DB storageService");
              let s1 = this.createUserTable()
              let s2 = this.database.executeSql("CREATE TABLE IF NOT EXISTS melding (idmelding INTEGER PRIMARY KEY AUTOINCREMENT, meldingType TEXT, meldingTitel TEXT, meldingOmschrijving TEXT, ruimte INTEGER, user_iduser INTEGER, FOREIGN KEY (user_iduser) REFERENCES user(iduser)); ", {});
  
              return Promise.all([s1, s2]).then(() =&gt; {
                  console.log('creating database complete!');
                  let i1 = this.insertUser();
                  let i2 = this.database.executeSql("INSERT INTO user (firstname, lastname, castletype, castleHealth) VALUES (?, ?, ?, ?)", ['Klaas', 'Janssen', 'mannelijk', '5']);
  
                  return Promise.all([i1, i2]).then((res) =&gt; {
                      console.log('all insert querys executed', res);
                  }).then(() =&gt; {
                      console.log('executing sql statement');
                      return this.database.executeSql('SELECT * FROM user', []);
  
                  }).then((resultSet) =&gt; {
                      console.log('after then((resultset): ' + resultSet);
                      console.log(JSON.stringify(resultSet, undefined, 2));
                      if (resultSet.res.rows.length &gt; 0) {
                          console.log('if statement true');
                          this.users = [];
                          for (let i = 0; i &lt; resultSet.res.rows.length; i++) {
                              console.log('forloop');
                              this.users.push({
                                  "iduser": resultSet.res.rows.item(i).iduser,
                                  "firstname": resultSet.res.rows.item(i).firstname,
                                  "lastname": resultSet.res.rows.item(i).lastname,
                              });
                          }
                          console.log('after forloop');
                      }
                  }).catch((error) =&gt; {
                      console.error("db error", error);
                  })
              })
          }, (error) =&gt; {
              console.log("ERROR oepening DB storageService: ", error);
          });
      });
  }
  
  public createUserTable() {
      this.database.executeSql("CREATE TABLE IF NOT EXISTS user (iduser INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT, castleType TEXT, castleHealth INTEGER); ", {}).then((data) =&gt; {
          console.log("USER TABLE CREATED: ", data);
      }, (error) =&gt; {
          console.error("USER Unable to execute sql", error);
      });
  }
  
  public insertUser() {
          this.database.executeSql("INSERT INTO user (firstname, lastname, castletype, castleHealth) VALUES (?, ?, ?, ?)", ['Jan', 'Klaassen', 'mannelijk', '10']).then((data) =&gt; {
              console.log('Insert Succesful');
          }, (error) =&gt; {
              console.error('user not inserted: ' + error);
          });
      }
  }
  </code></pre>
  
  <p><b>nexus 5 outputlog: </b></p>
  
  <pre><code>D/SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 6 :  StorageService Initialised
  I/chromium: [INFO:CONSOLE(6)] " StorageService Initialised", source: file:///android_asset/www/build/main.js (6)
  D/SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 6 : CastleService initialized
  I/chromium: [INFO:CONSOLE(6)] "CastleService initialized", source: file:///android_asset/www/build/main.js (6)
  D/SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 5 : SlimLabsService initialized
  I/chromium: [INFO:CONSOLE(5)] "SlimLabsService initialized", source: file:///android_asset/www/build/main.js (5)
  D/SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 5 : Logging in!
  I/chromium: [INFO:CONSOLE(5)] "Logging in!", source: file:///android_asset/www/build/main.js (5)
  12-20 07:48:04.042 796-18483/? W/InputMethodManagerService: Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@fd827af attribute=null, token = android.os.BinderProxy@7d616de
  D/CordovaWebViewImpl: onPageFinished(file:///android_asset/www/index.html)
  D/SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 19 : DEVICE READY FIRED AFTER
  I/chromium: [INFO:CONSOLE(19)] "DEVICE READY FIRED AFTER", source: file:///android_asset/www/build/main.js (19)
  I/App: WARNING: Back Button Default Behavior will be overridden.  The backbutton event will be fired!
  D/SystemWebChromeClient: file:///android_asset/www/plugins/cordova-sqlite-storage/www/SQLitePlugin.js: Line 175 : OPEN database: data.db
  I/chromium: [INFO:CONSOLE(175)] "OPEN database: data.db", source: file:///android_asset/www/plugins/cordova-sqlite-storage/www/SQLitePlugin.js (175)
  V/SQLitePlugin: Android db implementation: built-in android.database.sqlite package
  V/info: Open sqlite db: /data/user/0/com.ionicframework.cwistlev1105374/databases/data.db
  D/SystemWebChromeClient: file:///android_asset/www/plugins/cordova-sqlite-storage/www/SQLitePlugin.js: Line 179 : OPEN database: data.db - OK
  I/chromium: [INFO:CONSOLE(179)] "OPEN database: data.db - OK", source: file:///android_asset/www/plugins/cordova-sqlite-storage/www/SQLitePlugin.js (179)
  D/SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 6 : SUCCES Opening DB storageService
  I/chromium: [INFO:CONSOLE(6)] "SUCCES Opening DB storageService", source: file:///android_asset/www/build/main.js (6)
  D/SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 6 : USER TABLE CREATED: 
  I/chromium: [INFO:CONSOLE(6)] "USER TABLE CREATED: ", source: file:///android_asset/www/build/main.js (6)
  D/SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 6 : creating database complete!
  I/chromium: [INFO:CONSOLE(6)] "creating database complete!", source: file:///android_asset/www/build/main.js (6)
  D/SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 6 : Insert Succesful
  I/chromium: [INFO:CONSOLE(6)] "Insert Succesful", source: file:///android_asset/www/build/main.js (6)
  12-20 07:48:04.629 1596-1596/? I/Keyboard.Facilitator: onFinishInput()
  D/SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 6 : all insert querys executed
  I/chromium: [INFO:CONSOLE(6)] "all insert querys executed", source: file:///android_asset/www/build/main.js (6)
  D/SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 6 : executing sql statement
  I/chromium: [INFO:CONSOLE(6)] "executing sql statement", source: file:///android_asset/www/build/main.js (6)
  D/SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 6 : after then((resultset): [object Object]
  I/chromium: [INFO:CONSOLE(6)] "after then((resultset): [object Object]", source: file:///android_asset/www/build/main.js (6)
  D/SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 6 : {
                                                                                               "rows": {
                                                                                                 "length": 42
                                                                                               },
                                                                                               "rowsAffected": 0
                                                                                             }
  I/chromium: [INFO:CONSOLE(6)] "{
                                                                                  "rows": {
                                                                                    "length": 42
                                                                                  },
                                                                                  "rowsAffected": 0
                                                                                }", source: file:///android_asset/www/build/main.js (6)
  D/SystemWebChromeClient: file:///android_asset/www/build/main.js: Line 6 : db error
  I/chromium: [INFO:CONSOLE(6)] "db error", source: file:///android_asset/www/build/main.js (6)
  </code></pre>
  
  <p><b> additional information: </b>
  There isn't more information about the error or I don't know how to retrieve that. (not getting a stack trace or anything);
  Why doesn't it want to pass on the results! into the users[]?</p>