I am trying to build an app that will allow a user to scan a barcode, then save the time this barcode was scanned to native storage.
Then each time the user uses the application, they can continue to add successful scans (times) to native storage.
I have found that when I just set a date, it overwrites the previous date. So what I thought to do is take the existing date(s), add them to an array, add the new date to this array then put all this back into native storage.
However what I find is that when the user changes page, then returns to the scan page and adds a date to the array, its adding an entire new array into the old array as one variable rather than separate entries to this array.
How can I resolve this?
My code is -
export class ScanSession {
emptyArray = [];
ionViewDidEnter(){
this.nativeStorage.getItem(‘scans’)
.then(
data => this.emptyArray.push(JSON.parse(data)),
)
console.log(“Saved Scan Data Loaded”)
}
You said you have a problem with page change. Providers are running in the background not on each of your pages, so you can call them from every page, but they don’t create multiple instances of your variable.
(Bad explaination - it’s allready to late for me to think correctly…)
Anyways you do ionic g provider BarcodeTracker and then you import this new provider in your pages and access the functions on it that you moved to it(move all storage logic to it.)
I am trying to use this code, I’ve changed a little such as Storage to Native Storage as that’s what I’ve been using.
However I am getting an error saying cannot find name ‘platform’, and also property ‘ready’ does not exist on type ‘NativeStorage’.
Also just so my understanding is correct. Is the idea to call this function on a successful scan, and this will then add the scan to an array on native storage?
How do I call the function addScan from within my scanSession page? I thought I would just be able to go to my ScanCode function, and after the if check call it there, but it doesnt seem to work this way.
EDIT - Resolved platform error by importing platform.
EDIT2 - Resolved ready error by using storage, I shouldnt have changed your code.
I have imported BarcodeTracker at top of scanSession.
Included it in the constructor as - plublic barcodeTracker: BarcodeTracker
Checked app.module.ts, it automatically added it there but name was wrong so I changed this to be correct.
But when I now type addScan(); or even barcodeTracker.addScan, nothing is coming up? Says it cannot find it.
Thanks, I have got this piece working now.
I am getting some errors though. When I run the scanner it will successfully scan, but console shows this error - ERROR Error: Uncaught (in promise): [object Object]
I also tried using the getScans function to check data, I added a console.log(this.data); to it.
However when I run this I get these errors -
ERROR TypeError: co.ShowDataTest is not a function
ERROR CONTEXT DebugContext {view: {…}, nodeIndex: 30, nodeDef: {…}, elDef: {…}, elView: {…}}
How do I run a test to see what has been saved?
EDIT - Tried a simple -
ShowDataTest(): any{
this.nativeStorage.getItem(‘scans’)
.then(
data => console.log(data)
)
}
To test whats being saved. Get this error -
ERROR Error: Uncaught (in promise): [object Object]
Ahh I kind of see what you mean. But how then can I view whats been saved in native storage?
Also how do I resolve this error - ERROR Error: Uncaught (in promise): [object Object]
I am getting that error when I try to save to native storage, and also when I used -
ShowDataTest(): any{
this.nativeStorage.getItem(‘scans’)
.then(
data => console.log(data)
)
}
OK - I changed the ‘scans’ name, and deleted the app and reinstalled to clear the storage too.
What I’ve noticed is that as soon as I enter the scanSession page I get the error -
RROR Error: Uncaught (in promise): [object Object]
This error is given again when I use -
ShowDataTest(): any{
this.nativeStorage.getItem(‘newScans’)
.then(
data => console.log(data)
)
}
Also dotted .catch(console.error) around. Noticed that I get this error -
NativeStorageError {code: 2, source: “Native”, exception: null}
When I load scanSession page, or click the above code to show native storage saves.