I am facing a very strange issue with Ionic Storage. I have a method that reads a value from the storage and returns an a promise containing an object corresponding to that:
private getAuthorizedOptions(): Promise<RequestOptions>
{
return this._storage.get('bdAccessToken')
.then(v => {
console.log("access token: ", v);
let token = v;
let header = new Headers({
'Authorization': 'Bearer ' + token
});
let ro = new RequestOptions({
headers: header
});
let options = new RequestOptions();
if (options.headers) options.headers.delete("Authorization");
options.headers = header;
return options;
});
}
Now I have another method, which will call this above method twice within a chain of actions:
get(url:string, options?:RequestOptions): Observable<Response>
{
return Observable.fromPromise(this.getAuthorizedOptions())
.mergeMap((options) =>
{
return super.get(url, options)
.catch(err => {
if (err && err.status === 401)
{
return this._authService.refreshToken()
.mergeMap(r =>
{
return Observable.fromPromise(this.getAuthorizedOptions())
.mergeMap(opt => {
return super.get(url, opt)
});
}
)
.catch(err2 => {
console.log("redirecting.");
this.redirect();
return Observable.throw(err2);
});
}
else {
return Observable.throw(err);
}
});
});
}
Now tracing these methods shows something strange. The first time the “getAuthorizedOptions()” method is called, it can read the “bdAccessToken” value from the storage very well. The second time it is called, the returned value is NULL.
I have been pulling my hair for two days on this, any help is appreciated like you have never been appreciated before! lol!