I recently converted my Ionic 2 app to Ionic 3 implementing lazy loading for my pages. Other than adding modules for each page, I did not modify the code/logic.
Now I am running into an issue where I have a basic feedback form occasionally submitting twice to my API service backend. It does not happen every time but definitely more frequently when running on a device vs the browser. After adding logging, I noticed my feedback provider that handles the API Post is initializing twice when the feedback page is opened.
I’ve tried moving the provider to be declared in the app module as well as within the page module to see if the lazy loading was causing an issue but no luck.
import { NgModule } from ‘@angular/core’;
import { IonicPageModule } from ‘ionic-angular’;
import { FeedbackPage } from ‘./feedback’;
import { FeedbackProvider } from ‘@providers/feedback/feedback’;
I don’t understand the source of your problem yet, but I know this isn’t the fix. If you have a module that contains providers, import the module into app.module.ts with a static forRoot() function that returns a ModuleWithProviders, so Angular knows to register the provider. You can lazy load the other parts of your module again later.
Edit: Like this. I’m not sure where I found this in the Angular docs, but it’s there.
After further research, I’ve discovered it’s not related to the provider at all. It appears whenever I make a POST to the server, it’s making two calls. So I still have a problem but it’s not an issue of the provider getting initialized twice.
Ya, I noticed that. My problem is that my API endpoint is retrieving/processing the request twice. I was under the impression the OPTIONS request didn’t contain a payload and IIS would handle it. Instead the API controller endpoint is actually processing the call twice so it must have a payload with it or it’s some other issue.
This is a real long shot on an old thread, but if anybody actually does come across the same problem OP had (not the preceding post, which is totally unrelated), if you subscribe to HTTP calls in multiple places, this can cause the underlying request to be resubmitted on each subscription. I think this is caused by HttpClient dealing in cold observables, and it bit me when I was trying to watch upload progress. What worked for me was to add share to the end of the pipeline of the process generating the HTTP request.
Can you please explain/show how this " add share to the end of the pipeline" is used I am doing few simple API or local assets HTTPClient.get() requests and later subscribing to the Observable they return.
Your comment caught my attention, as I am now facing duplicate constructor calls to components (not services) that should be only created once, like ones rooted on the tab pages.
I am not using lazy loading.
Thank you.
Both afo and bfo return a very similar-looking Observable from the outside world, but a new HTTP request will be fired every time you subscribe to the one coming back from afo. This is not true for bfo: a single call to bfo generates one HTTP request, no matter how many places you subscribe to it.