Tachyon
Tachyon

Reputation: 2411

Ionic 3 Sqlite Returning Null

I am trying to read out of my Sqlite database in Ionic 3 but the returned value is always null. If I console.log(data.rows.item(0).token) in the method then it logs the correct value to the console but doesn't return the correct value to the calling code.

This is the method I am using to get the token:

getToken() {
    //Open database
    this.openCreateDatabase()
      .then((db: SQLiteObject) => {
        //Execute select
        db.executeSql("SELECT token FROM Token WHERE id = 1", [])
          .then((data) => {
            //Get and return the token
            return data.rows.item(0).token;
          })
          .catch((e) => {
            //Log any errors
            console.log(JSON.stringify(e));
            return null;
          })
      });
  }

The calling code is simply:

ionViewDidEnter(){
  console.log(this.sql.getToken());
}

Upvotes: 0

Views: 292

Answers (3)

Tachyon
Tachyon

Reputation: 2411

IftekharDani pointed me in the right direction by mentioning promises, although his solution didn't work directly it helped me develop one that did. The code I ended up with is as follows:

getToken() {
    return new Promise((resolve, reject) => {
      this.openCreateDatabase()
        .then((db: SQLiteObject) => {
          db.executeSql("SELECT * FROM Token WHERE id = 1", [])
            .then((data) => {
              let tokenValues = [];
              if (data.rows.length > 0) {
                for (let i = 0; i < data.rows.length; i++) {
                  tokenValues.push(data.rows.item(i).token);
                }
              }
              resolve(tokenValues);
            }, (error) => {
              reject(error);
            })
        });
    });
  }

Upvotes: 0

IftekharDani
IftekharDani

Reputation: 3729

For getting token you need to make getToken as Promise.

Add return before openCreateDatabase;

getToken() {
    //Open database
    //
    return this.openCreateDatabase()
      .then((db: SQLiteObject) => {
        //Execute select
        db.executeSql("SELECT token FROM Token WHERE id = 1", [])
          .then((data) => {
            //Get and return the token
            return data.rows.item(0).token;
          })
          .catch((e) => {
            //Log any errors
            console.log(JSON.stringify(e));
            return null;
          })
      });
  }

Change ionViewDidEnter with :

1.async await

async ionViewDidEnter(){
 let token = await this.sql.getToken();
  console.log(token);
}

2. Or Then

ionViewDidEnter(){
     this.sql.getToken().then(token => {
console.log(token)
     })
    .....
    }

Upvotes: 1

user7825545
user7825545

Reputation:

Just try to create a method local variable or a js file global variable and return that one. Something like this:

getToken() {
//Open database
this.openCreateDatabase()
  .then((db: SQLiteObject) => {
    //Execute select
    db.executeSql("SELECT token FROM Token WHERE id = 1", [])
      .then((data) => {
        //Get and return the token
        let val = data.rows.item(0).token;
        return val;
      })
      .catch((e) => {
        //Log any errors
        console.log(JSON.stringify(e));
        return null;
      })
  });
  }

Tell us if that works.

Upvotes: 0

Related Questions