Avatar of detox1978
detox1978Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

run code within JSON **URGENT**

Hi All,

I've been asked to write a mobile app for a customer and as usual I've left it to the last minute, which is now tomorrow.

The code below is made up of two parts.

The first part is a getJSON() query that returns data as three strings sql_id, sql_company, sql_con.  This part works well.

The second part is to save the data collected in part one into an IndexedDB, which doesn't work.

$.getJSON(url, function(data){
    $.each(data.users, function(i,user){
         //set variables
         var sql_id = user.id;
         var sql_company = user.company;
         var sql_contact = user.contact;

         //put variables into IndexedDB
         store.put({id: sql_id, customer: sql_company, contact: sql_contact});
    });
});

Open in new window



Does anyone know how I can get the store.put to work?

The console logs the following error
Uncaught TransactionInactiveError: Failed to execute 'put' on 'IDBObjectStore': The transaction has finished.

Open in new window


If I run store.put outside of the getJSON(), it works.


Any suggestions?
JavaScriptAJAXjQuery

Avatar of undefined
Last Comment
detox1978
ASKER CERTIFIED SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of detox1978
detox1978
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Yes I am trying to add a new record.

store.add({id: sql_id, customer: sql_company, contact: sql_contact});

Open in new window


returns
Uncaught TransactionInactiveError: Failed to execute 'add' on 'IDBObjectStore': The transaction has finished.
Avatar of detox1978
detox1978
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I'm getting a bit desperate, so tried switching to WebSQL (instead of IndexedDB).

tx.executeSql('INSERT INTO CRM (id, customer, contact, modified) VALUES (1, "Company name","Contact Name","2014-01-01")');

Open in new window


returns this
Uncaught InvalidStateError: Failed to execute 'executeSql' on 'SQLTransaction': SQL execution is disallowed.
Avatar of detox1978
detox1978
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I managed to get WebSQL to work by closing the initial create and starting a new one for the inserts

db.transaction(function (tx) {
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo