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';

selector: 'page-scanSession',
templateUrl: 'scanSession.html'
export class ScanSession {

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

     //Test to see if data is being saved
     ShowDataTest(): any{
        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){
  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';

export class BarcodeTracker {


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

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


public getScans(){

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

working project here for 7days:

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

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