So can you please correct me if i am wrong …
//Its working fine
this.storage.get(‘user_id’).then((result) => {
console.log(result);
});
//its not working
this.storage.get(‘user_id’).then((result) => {
});
console.log(result);
So can you please correct me if i am wrong …
//Its working fine
this.storage.get(‘user_id’).then((result) => {
console.log(result);
});
//its not working
this.storage.get(‘user_id’).then((result) => {
});
console.log(result);
Not sure what you’re asking here. Looks like there could be a syntax error?
Your console.log line is not within the promise callback, so it wouldn’t work in the second scenario.
Thanks for your reply @tumain this is my actual code
getstorage()
{
this.storage.get(‘user_id’).then((result) => {
console.log(result);
});
}
let value =this.getstorage();
//then my output is like this
ZoneAwarePromise {__zone_symbol__state: null, __zone_symbol__value: Array(0)}
__zone_symbol__state: true
__zone_symbol__value: “5”
proto: Object
then how can i get this value __zone_symbol__value: “5”
i prefer the async/await syntax for promises, it feels easier to read
the method needs to return
some value if you are assigning it to a variable
async getStorage(): Proimise<any> {
try {
const result = await this.storage.get('user_id');
console.log(result);
return result;
}
catch(e) { console.log(e) }
}
let value = this.getStorage();
alternatively, without async:
getStorage() {
return this.storage.get('user_id').then((result) => {
console.log(result);
return result;
});
}
let value = this.getStorage();
do either of these work?
Thanks @jjdev Checked
Nope!
same output i got
ZoneAwarePromise {__zone_symbol__state: null, __zone_symbol__value: Array(0)}
__zone_symbol__state: true
__zone_symbol__value: “5”
proto: Object
let value: any;
getStorage() {
try { return await this.storage.get('user_id') }
catch(e) { console.log(e) }
}
this.getStorage().then((result) => {
if (result) {
console.log(result);
value = result;
}
});
Here I have just returned the promise entirely, and handle the ‘then’ outside of the method.
I’m not sure why this isn’t working for you, I haven’t had many problems using localStorage this way
Thanks @jjdev I tried all your ways but only got this output
ZoneAwarePromise {__zone_symbol__state: null, __zone_symbol__value: Array(0)}
__zone_symbol__state: true
__zone_symbol__value: “5”
proto: Object
Anyone having a idea ???
Please try this,
let x = storage.get(‘user_id’)
I would just double check here and make sure you have everything implemented properly
Try this.
var result: any;
this.storage.get(‘user_id’).then((data) => {
result = data
});
console.log(result);
you do not use a promise value directly.
you either use it in it’s then/catch method with callback,
or use it with await inside async block.
you may learn more about async programming in javascript from this youtube video:
Could you find any answer? I have same issue, but only when I build the app with capacitor, on the browser works fine
yes
it is really easy for start in you`re service create a function and then paste below text on that. for example i have a method to show PROFILE info in USER-STORAGE service
//read user to show in profile pages
//Iuser this my interface you can see on below
// this method return Promise
read_to_show_profile(): Promise {
return this.storage.get(user_storage_DB);
}
//
export interface Iuser {
id: string;
name: string;
mobileNumber: number;
city: string;
subCity: string;
galleryName: string;
imageSrc: string;
}
/*/
and then in you`re component u can import SERVICE and use it like below
MY.COMPONENT.TS file
public current_usr: Iuser = {};
constructor(
private storage_service: StorageUserService
) {}
ngOnInit() {
//read data from local storage and assign it to CURRENT_USER
this.storage_service.read_to_show_profile().then(result => {
this.current_usr = result;
console.log(this.current_usr);
});