سؤال

I am currently building a offline app which will be used on mobiles they will be uploading lots of data.

Everything works great, but I would like to remove the objects out of the database when they upload successfully.

I have * where it should be doing that, any one have ideas why this is not working?

function uploadData(e) {

       var transaction = db.transaction(["data"], "readonly");
       var objectStore = transaction.objectStore("data");

       var cursor = objectStore.openCursor();

       cursor.onsuccess = function(e) {
           var res = e.target.result;
           if(res) {

               if (navigator.onLine) {
                var passData = {

                        offer: res.value.offer,
                        created: res.value.created,
                        image: res.value.image,
                    };

                var jsonData = JSON.stringify(passData);

                $.ajax({
                       url: "{{ path('destination_app_ajax') }}",
                       type: "post",
                       data: {
                           "json": passData 
                       },
                       success: function(JsonData) {

                            *This should delete out of indexeddb
                            transaction.objectStore("data").delete(res.key); 


                       },
                       error: function() {
                           $('.popup-heading').text('Some items didn't upload try again!');

                       }
                   });


               }else{
                   $('.popup-heading').text('Please find stronger signal or wifi connection');
               }

               res.continue();    

           }
       }     

   }
هل كانت مفيدة؟

المحلول

When you open your transaction on the data object store, you're opening it as readonly:

   var transaction = db.transaction(["data"], "readonly");
   var objectStore = transaction.objectStore("data");

I believe you're looking for readwrite. readonly transaction mode should never be allowed to remove objects. The versionchange transaction can do both, but should probably only be used when actually modifying a database schema and not overloaded to also insert entries.

One tip: Leave the initial tx as readonly and open a new one for the object removal. Reusing transactions, it's easy to run into headaches when transactions start committing without your explicit doing so.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top