Adding an array to native storage

I hadnt actually installed storage, but I have done that now. Still getting the same error.

I did try a few things out though. If I go back to my original functions of storage, but pushing it to an array thats on the providers page, and removing the json.stringify from the push, well it stores correctly. And holds data between saves and moving around pages.

However, if I close the app then reopen the app and add a new scan, it then overwrites the previously saved data. So im guessing when the app is loaded its recreating the array in the provider. Would it be easier to find a way to add native storage scans to this array and go with this method? How could I do that?

I really cant get the code you’re showing me to work. Everything its returning the error -
NativeStorageError {code: 2, source: “Native”, exception: null}

But then again I can get mine to hold data between pages now, but cannot get it to maintain data between app loads.

I’m really stuck :confused:

can you sent me your code of your page and your provider?

Page -

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { BarcodeScanner } from '@ionic-native/barcode-scanner';
import { DateTime } from 'ionic-angular/components/datetime/datetime';
import { NativeStorage } from '@ionic-native/native-storage';
import { HomePage } from '../home/home';
import { BarcodeTracker } from '../../providers/barcode-tracker/barcode-tracker';
import { Storage } from '@ionic/storage';

@Component({
selector: 'page-scanSession',
templateUrl: 'scanSession.html'
})
export class ScanSession {

constructor(private barcodeScanner: BarcodeScanner, private nativeStorage: NativeStorage, 
public barcodeTracker: BarcodeTracker, private storage: Storage ) {   
  this.storage = storage;
   }
     

     //Test to see if data is being saved
     ShowDataTest(): any{
       this.nativeStorage.getItem('scans')
       .then( 
        data => console.log(data),
        error => console.log(error)            
        );
       }        
    
ScanCode() : any{

this.barcodeScanner.scan().then((barcodeData) => {
let inputString = "testData";
let dateStamp = Date();

if( barcodeData.text == inputString){
  
  this.barcodeTracker.addScan;
  
  console.log("Successfully added");

} else {
  console.log("Doesnt Match");
  }
    
 }, (err) => {

 });
 };
 }

Provider -

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
import { NativeStorage } from '@ionic-native/native-storage';
import { Platform } from 'ionic-angular/platform/platform';

@Injectable()
export class BarcodeTracker {

data:{scantime:Date,scanvalue:number}[]=[];

constructor(private nativeStorage: NativeStorage, public platform: Platform, private storage: Storage ) {

platform.ready().then(() => {
      this.storage.ready().then( _ => {
      this.nativeStorage.getItem('newScans').then((d)=>{
        this.data = (d != null)?d:[];

      })
    })
  })
  }

public getScans(){
console.log(this.data);
return this.data;
}

 // This function returns a promise, so you can catch errors on your page
public addScan(time:Date,value:number):Promise<any> {
this.data.push( {scantime:time, scanvalue:value} );
return this.nativeStorage.setItem('newScans', this.data);
}
}

working project here for 7days: https://we.tl/fU68P6Oz9X

You have some mistakes in your code, as example you use sth called this.nativeStorage.getItem instead of this.storage.get

You are amazing! Thank you so much.
I will need to modify it slightly, but it works perfectly. Thanks again!!