Check a key from sqlite storage exist or no?


#1

what if i want to check if the key exist or no before i try to remove it from storage,
because i tried to remove the storage from the browse manually and then try to log out , the logout function try to remove the token from storage but didn’t find it : i have the following error:
Uncaught (in promise): InvalidStateError: Failed to execute ‘transaction’ on ‘IDBDatabase’: The database connection is closing.
i’m using "cordova-sqlite-storage"
import {Storage} from ‘@ionic/storage’;


#2

You could try and fetch it from the database before tryingh te remove it. That’s the same as checking whether the key exists. If your response is empty/null/undefined or returning with an answer that tells you the key doesn’t exist, you know you don’t have to take any action. If it comes back with an answer, you can remove the key.


#3

i tried it also not working :

 public logout() {
    this.storage
      .ready()
      .then(
        () => {
          this.getToken().then(
                 (token)=>{
                     if(token){
                          this.storage.remove(this.appConfig.Access_Token);
                          this.storage.remove(this.appConfig.User_Info);
                      }
                 }
          );
         }
      )
  } 


public getToken() {
    return this.storage
      .ready()
      .then(
        () => {
          return this.storage.get(this.appConfig.Access_Token);
        }
      )
  }

#4

Could you please reformat your code by using the < /> formatter on your code? It’s easier to read for us on the forum. You’re checking if(token), but that probably should check whether it’s not null also: if(!!token) . Token always exists, since that’s the result you’re getting from the store. The result could still be empty though, so you should check for that.


#5

i don’t understand what you mean . i checked if the token variable has value not null so remove it.
what’s the wrong here?


#6

I noticed that removed the database from F12 >> application >> IndexDB >> delete database
instead of clear the keys.
so i think because of that the error appear,
but i wonder if the user delete cache on his phone , the database of the app also deleted ? or just clear the cache? i’m trying it on the phone currently


#7

I know for one thing that if you get the result like this:

(result) => {

}

that if you check within that lambda if (result) result always exists. So you should check whether it has a value or not to be certain if the result is really empty and you should act on it. If you’re removing the database like that, it off course deletes your database. I think that the sqLite db is persistent for users of a phone, but I’m not sure.


#8
  1. result is the response from promise so result itself the value so if(result) check if it is exist or have any value like result=‘1’ or = ‘s’ , or result=true ,
    2)on the phone the sql lite db on clear cache still persist.
    so no need to do if statement and call getToken to see if it’s exist or no :
public logout() {
    this.storage
      .ready()
      .then(
        () => {
          this.storage.remove(this.appConfig.Access_Token);
          this.storage.remove(this.appConfig.User_Info);
        }
      )
  }

it solved