Search code examples
indexeddbintel-xdk

IndexedDB function is not working in Intel XDK


I have made a web app that calls two indexeddb databases and when click a button it saves a value and goes to another page.

It works perfect as a web app but when I try to use it with Intel XDK it shows in the console: "Uncaught TypeError: Cannot set property 'varBusquedaPAM' of undefined". This is the Javascript:

var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
        var dataBase = null;
        var bdBusqueda = null;

        function iniciarBD() {
            dataBase = indexedDB.open('bdbeta', 4);

            dataBase.onupgradeneeded = function (e) {
                var active = dataBase.result;
                var objetoProductos = active.createObjectStore("productos", {keyPath: 'id', autoIncrement: true});
                objetoProductos.createIndex('Nombre_Producto', 'producto', {unique: true});
            };

            dataBase.onsuccess = function (e) {
                varBusqueda();
            };
            dataBase.onerror = function (e) {
                alert('Error al cargar la base de datos');
            };
        }

        function varBusqueda() {
            bdBusqueda= indexedDB.open('bdbeta2', 1);

            bdBusqueda.onupgradeneeded = function (e) {
                var active = bdBusqueda.result;
                var objetoLista = active.createObjectStore("variableBusqueda", {keyPath: 'id', autoIncrement: true});
                objetoLista.createIndex('Variable_de_Busqueda_Producto', 'varBusquedaPAM', {unique: true});
                objetoLista.createIndex('Variable_de_Busqueda_Marca', 'varBusquedaMAM', {unique: true});
                objetoLista.createIndex('Variable_de_Busqueda_Modelo', 'varBusquedaMAI', {unique: true});
            };

            bdBusqueda.onsuccess = function (e) {
                //Do nothing
            };
            bdBusqueda.onerror = function (e) {
                alert("Hubo un problema al conectar a la base de datos");
            };
        }

        function irMarcas(id){
            var active = bdBusqueda.result;
            var data = active.transaction(["variableBusqueda"], "readwrite");
            var object = data.objectStore("variableBusqueda");
            var request = object.get(1);

            request.onsuccess = function() {
                var data = request.result;

                data.varBusquedaPAM = id; // <---HERE IS WHERE THE ERROR IS SHOWN

                var consultaActualizar = object.put(data);
                consultaActualizar.onsuccess = function() {
                    window.location.href = "marca.html";
                };
            };
        }

And I call the function "irMarcas(id)" form this html tag:

<input type="button" id="TM" value="Tarjetas madre" onclick="irMarcas(this.id);">

I did some similar projects in the past with the same functions (indexedDB) and I never got this errors before. Just in case my version of Intel XDK is the 3088.

Did I forgot something?

Thank you all.


Solution

  • The error means your data variable is undefined. The data variable is undefined because object.get(1) did not match any objects in your object store.