Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loki.js lost data in Ionic Apps

I am developing an Ionic app using loki.js, but every time it refresh the app press f5 i loss all data stored in loki database. Why it happen?

I using no chache in my ionic app.

like image 594
SAMUEL OSPINA Avatar asked Oct 30 '22 09:10

SAMUEL OSPINA


1 Answers

It could be that when you press F5 the data saved in memory is not written to the target json file yet. You can try to set explicitly a time range to save the data when you instantiate loki:

var _db = new Loki('./database/db.json', {
            autoload: true,
            autosave: true,
            autosaveInterval: 5000 // 5 secs
        });

function add(newPatient) {
        return $q(function(resolve, reject) {
            try {                        
                    var _patientsColl = GetCollection(dbCollections.PATIENTS);
                    if (!_patientsColl) {
                        _patientsColl = _db.addCollection(dbCollections.PATIENTS,{indices:['firstname','lastname']});
                    }
                    _patientsColl.insert(newPatient);
                    console.log('Collection data: ', _patientsColl.data);
                    resolve();
            }
            catch (err) {
                reject(err);
            }
        });
    }

function GetCollection(collectionName){
        return  _db.getCollection(collectionName);
    }

With "autosaveInterval" the data in memory will be written to the JSON file every 5 seconds (you can adjust this value as you prefer).

EDIT I added the code I use to save a new document into my collection and even with page refresh, the data is stored correctly. I use "autosave" among the db settings, maybe you can set it as well, in case there is a path that is not correctly catch when you explicitly trigger saving.

like image 194
Francesco Avatar answered Nov 08 '22 10:11

Francesco