Android SDK Tools : 26.1.1 (/home/peter/.android/android-sdk)
NodeJS : v12.18.2 (/usr/bin/node)
npm : 6.14.7
OS : Linux 5.7
The problem is that now the local storage does not work when I test the app in the browser using ionic serve --browser chromium. It has been working fine for several years before this. I have not tried it on the phone, the phone works fine on the previous version. There is nothing stored on Web SQL or IndexedDB and when I console.log storage the driver is null.
The storage is set on login with:
this.storage.set(âcurrentUserâ, JSON.parse(JSON.stringify(newUser)));
The storage is queried with:
this.getUserInfo().then((userObj) => { ⌠});
which calls:
public getUserInfo() : any {
return this.storage.get(âcurrentUserâ);
}
Now this doesnât work on browser and complains that userObj is undefined.
This is a pain as every time I make a change when running in the browser, I have to login again to see the results of the change.
Any ideas?
I have done a few more investigations without much progress.
I tried on a different browser.
I then added more detail to the code:
this.storage.ready().then(() => {
console.log(âStorage Readyâ);
this.storage.set(âcurrentUserâ, JSON.stringify(newUser));
this.storage.set(âversionInfoâ, this.appConfig.VersionInfo);
console.log(this.storage);
this.storage.length().then((obj) => {
console.log(obj);
});
});
This returns on the console log:
Storage Ready
Storage {_driver: null, _dbPromise: ZoneAwarePromise}_dbPromise: ZoneAwarePromise {__zone_symbol__state: true, __zone_symbol__value: {âŚ}}_driver: nulldriver: (âŚ)proto: Object
0
So after the driver is ready storage is null and after adding some objects the index is 0.
Please, please does anyone have any thoughts on this?
No, I do not use github for this. I put all the relevant code in the description. What else do you need to know. As I said the code has worked for some time until the infrastructure was updated.
The only things which might be missing are the app_module:
import { IonicStorageModule } from â@ionic/storageâ;
IonicStorageModule.forRoot({name:â__nameâ, driverOrder: [âindexeddbâ, âsqliteâ, âwebsqlâ]}),
and the service:
import { Storage } from â@ionic/storageâ;
constructor(
public http: HttpClient,
public network: Network,
public appConfig: AppConfigService,
private storage: Storage
) { }
I have re-enabled the storage by going back to version 2.2.0 of @ionic/storage, the problem seems to be with the new noop code in version 2.3.0.
I had to examine the code to find the likely problem and then try it. This took me about 2 days of effort that I can ill afford.
How should we adapt our code to make this work on the latest version? Shouldnât updates include instructions if the code needs changing - this is what I get with linux.
As you probably discovered figuring this part out, that triggers if window.process is defined. So is there a possibility that youâve got some dependent library in your project that is defining it? If so, can you eliminate that dependency? If not, can you try undefining it? First place I would attempt to do so would be in main.ts right before bootstrapping the app.
I did a search for window.process within the whole of my app directory using the Find in Files⌠facility in Gedit. The only files that this appears are in node-modules. Most of these are part of a useColors function, exceptions are the isInBrowser function in support-map-support.js, and the resolve function in resolve.js. All of these are part of âifâ statements so should not be setting anything.
<ion-content>
<ion-item>
<ion-label>n is {{n}}</ion-label>
</ion-item>
<ion-button (click)="bump()">bump</ion-button>
</ion-content>
âŚand I see your symptoms: n becomes blank on the first click of the âbumpâ button, and never recovers.
All of that being said, the code you have posted is still problematic, and I would urge you to rearchitect it. The primary issue here is that storage writes are not synchronous. I can think of two ways around this: one is to meticulously wait on the promises returned by every set call. That is a massive PITA in my book, and also causes needless performance hiccups.
A better approach, I think, is to use storage only to communicate between app runs, not within one. So only read from storage once, at app startup, or whenever the information is first needed. Write whenever you like. For in-app communication, use mutually injected service providers to share data, not storage.
I tried your fix but it didnât change anything. I also tried a test app, which worked. I have also searched for the pattern within my app directory without success. So I am sticking with version 2.2.0 for now until I can determine what the root cause actually is.
Actually, the storage is primarily used for user information that enables them to automatically logon to the server where the bulk of the information and processing is. There is a service in-place for exchanging data between pages and the design minimises that information exchange.
With the fault, I have revisited the storage code such that at first access the object is stored as part of the service so the storage is only used at the start when read and when the objects change - which is rarely. The primary setting point is user login.
If you cannot replicate the issue in a new project, itâs not going to be possible to debug. If you end up being able to replicate and can provide a sample project, please do.
On my failing app I managed to look at the value of the process. I have a screen shot but it will not let me upload; I have tried png and jpg without success (from linux). The process object is filled with a bunch of NOOP elements and has been produced by the browser.js function (node-modules.process.browser.js). So the process is undefined, it is just filled with NOOPs. The function suggested to clear the process doesnât work. I cannot figure out what is calling the browser.js function or why it is being called. There are a number of modules that incorporate it, e.g. www/vendor-es.js. I use mermaid.js within my app, this does incorporate the browser.js function but I have included it in my test app and it has no impact on the process. Why is storage bothered about the process?
Please just provide a demo app that has the same issue. I do not need you to look into further, I can manage that. I need you to provide an example that has the issue youâve mentioned.
More specifically, this is the relevant code that the AWS Amplify library wants us to add as part of the setup, to src/polyfills.js:
(window as any).global = window;
(window as any).process = {
env: { DEBUG: undefined },
};
Iâll look into whether i can âeliminate that dependencyâ, but Iâm not sure. In the meantime, I think I can easily create a repro so will try that first.
Brilliant! Thatâs exactly the sort of non-pathological example I was looking for. I would advise you to temporarily pin @ionic/storage at 2.2.0 in your package.json and watch the discussion on this commit, because I suspect the endgame here is for Ionic to change the way it attempts to detect SSR. I came across the SO thread that kygoh posted as a comment there during my detective endeavour here, and it does indeed strike me as a more robust method.
Thanks for the suggestion to roll back one version to 2.2.0. Before reading your suggestion, I did end up reproducing the problem in a minimal app here:
Unfortunately, going to version 2.2.0 did not solve this issue, as you can see in the reproduced repository above, which Iâve updated to go back to version 2.2.0 of @ionic/storage, but which still shows the same issue reported here. To see the issue in action:
Run the app in the repo above and check the DEV console - youâll see result = undefined in DEV console.
Now comment out the line that sets window.process in src/polyfills.js and youâll see result = test-value
The place in the code that prints out result = .. is in app.component.ts.