This.storage.length()

Hi, I would like to know how much value there is in my local storage.

hmm… try:

this.storage.length().then(result =>{
console.log(result);
});

If you want to check whether your data exist, then:

this.storage.get('data_key').then(your_data =>{}).catch(err=>{
console.log('Your data don't exist and returns error in catch: ' + JSON.stringify(err);
});
2 Likes

ok, thanks, good, but how to have this value in a variable ? like this :

let lengthStorage = this.storage.length().then(result =>{
return result.length;
});
console.log(lengthStorage);

not working, I know, but what is the correct syntax ?

The short answer that will save you lots of frustration is “don’t”. Only rely on it from within the then clause.

it does not work that way. if it is a ordinary function like:
let a = function my_func(){
return something;
}

this works. but when using ‘.then()’ you have to deal with it within the '{}'
for example:

this.storage.length().then(result =>{
//do everything you need to do with the result
});

or, you can set var outside the this.storage.length() and assign the data when you get it.
for example(inside your page.ts class:

thedata;
    this.storage.length().then(result =>{
    this.thedata = result;
    });

*you dont ‘let’ when setting variable inside your page class. unless it is in your constructor.

1 Like