Loki.js lost data in Ionic Apps

I am developing an Ionic application using loki.js, but each time it updates the application, press f5 i, having lost all the data stored in the loki database. Why is this happening?

I do not use chache in my ionic application.

+5
source share
2 answers

Perhaps when you press F5, the data stored in memory is not yet written to the target json file. You can try to set an explicit time range for saving data when creating a loki instance:

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 configure this value as you wish).

, , . "" db, , , , , .

+2

/Users/xxx ,, , .

0

Source: https://habr.com/ru/post/1629735/


All Articles