I have the following piece of code
this.http.get(version).pipe(
map(res => res.json())).subscribe(data => {
this.storage.ready().then(()=> {
this.storage.set('version', data);
currentVersion = 1;
if(currentVersion < data) {
this.storage.set('version', data);
this.http.get(website1).pipe(
map(res => res.json())).subscribe(data => {
this.storage.set('website1', data.items);
});
this.router.navigateByUrl('/app/tabs/outsideTheLoadingScreen');
});
}
);
Which works well with ionic serve, I get it working even with cordova in browser, it fails when I try to go back in history, but still loads all the content.
I added access and allow-navigation to the config.xml and also the plugins whitelist and advanced http.
Does anyone has any clues about this?
Can it be the fact that I use ionic 3 instead of ionic 4?
When are you reading from storage?
After I start the app, I check in ionViewWillEnter()
So regarding the storage.set
calls in the code you posted: can you confirm that you are not expecting to be able to read this information until the next time the app has been quit and restarted?
For the version variable ,yes, I check it only when the app is started.
OK, then can you be more explicit about what “not working” means? What are you expecting to happen? What actually happens? How are those things different?
Sorry if I wasn’t clear from the beginning. The code will check for the version so if this.http.get(version) will not return something then execution of the code will not continue, the version that I get doesn’t matter since the currentVersion < data always returns true.
I asked three specific questions, and am not seeing an answer to any of them.
What are you expecting to happen?
What actually happens?
How are those two different?
I expect both http.get to return a promise. If first http.get doesn’t return anything then the app will be stuck into loading screen and that is happening. If the first http.get will return a value then the loading screen will dissapear.
I don’t see anything involving loading screens in what you have posted. Can you provide a minimal complete example that people can use to reproduce your issue?
The page where this is executed is considered a loading page, after the http.get returns the promise then _this.router.navigateByUrl(’/app/tabs/(schedule:schedule)’); is triggered, I updated the code.
I am sure that the problem is from http.get. I compared my code with another project where this works and I didn’t saw my problem, I think that it comes from some settings.
Sorry, but apparently I’m not communicating with you very well, so somebody else is going to have to take over.
Im not going to attempt a specific answer, more some rules of thumb Ive found that work for me:
if promises are involved, putting logic in the .then makes for code that is difficult to debug and maintain. I recommend you refactor it out, along the lines of this sloppy and incomplete pseudo code
http.get ( .. stuff ..).then(res => {
this.somename(res);
}, (err) => {
... process error
})
somename(data) {
if (do some logic) {
storage.set _// ??? does this return a promise ???_
}
}