5

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.

SAMUEL OSPINA
  • 321
  • 2
  • 9
  • adding to @Luca 's answer, did you make sure there's a `db.saveDatabase()` call at any stage before you refresh the page? if the db isn't saved it won't persist - obviously. – Joe Minichino Feb 21 '16 at 17:43
  • i did it and i lost the data here is my code: `function addUser(user) { _user.insert(user); _db.saveDatabase(); };` – SAMUEL OSPINA Feb 21 '16 at 17:58
  • shloud i use a persitense adapter? – SAMUEL OSPINA Feb 21 '16 at 18:19
  • 2
    I also have this problem, but only for some collections in other everything works perfectly – Iago Leão Mar 11 '16 at 15:10
  • I am experiencing the same issue : I have 3 collections, 2 are working fine and the last one is never saved ... The same code is used for the 3 collections. – Paul Apr 17 '16 at 12:07

2 Answers2

2

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.

Francesco
  • 9,947
  • 7
  • 67
  • 110
0

Put the file in the /Users/xxx directory,then magical things happened, the program is running normally.

BDL
  • 21,052
  • 22
  • 49
  • 55
Jun He
  • 1