Reputation: 111
I've a problem adding a record from a form.
When I do clic in a button call to function "save":
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
var dataBase = null;
function save() {
dataBase = indexedDB.open("bbdd", 1);
dataBase.onupgradeneeded = function (e) {
active = dataBase.result;
};
dataBase.onsuccess = function (e) {
console.log('Database loaded!');
var active = dataBase.result;
var data = active.transaction(["docs"], "readwrite");
var object = data.objectStore("docs");
var request = object.put({
idDoc: document.querySelector("#idDoc").value,
name: document.querySelector("#name").value,
});
request.onerror = function (e) {
alert(request.error.name + '\n\n' + request.error.message);
};
data.oncomplete = function (e) {
idDoc: document.querySelector("#idDoc").value = '';
name: document.querySelector("#name").value ='';
};
}
dataBase.onerror = function (e) {
console.log('Error loading database');
};
}
Ok. I testing in Chrome and Firefox (Windows XP) and works fine. Data are added to database and fields are cleared.
Now, I built cordova project and run it in a Nexus 5 (Android 5.0.1) with:
cordova run android --devices="myId"
In Nexus 5 donesn't work. When I do click in the button the mobile phone does nothing. Data aren't added to database and fields aren't cleared.
What am I doing wrong?
Upvotes: 0
Views: 347
Reputation: 11
I have this problem, too. There seems to be a bug on Android 5.0.1 related to the browser storage. This problem happens with localStorage, webSQL and indexDB. A lot of people are experiencing this: Android 5.0.1 localstorage not persistent.
I made a simple app just for testing. I open the app and I just simply do (I added the code when onDeviceReady
- when cordova is loaded):
var a = window.localStorage.getItem('test');
alert(a); //will give you undefined first time
if (!a) {
window.localStorage.setItem('test','12345678');
alert(windows.localStorage.getItem('test');
//this will display 12345678 from localstorage evertime
}
If you close the app after 2 or 3 seconds (force-closing the app) and re-open it, the value of test
from localstorage is still undefined
. You can do this for 10 times and you will get undefined
every time. If you wait 10 or 12 seconds after you open the app and get the alerts, you will notice that after restarting the app (force restart), the data is there and localStorage works correctly.
I tested this on Android 5.0.1 and I used cordova 3.6.3, cordova 4.0, cordova 5.0. I did the same test on a tablet with Android 4.4.2 and the localStorage is set correctly.
Upvotes: 1