Solved

run code within JSON **URGENT**

Posted on 2014-12-02
4
400 Views
Last Modified: 2014-12-02
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?
0
Comment
Question by:detox1978
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 40477275
Hi mate.

So you're trying to insert a new record or update an existing one?
First thing is that you're using the 'put' method which updates an existing record and for that you need to get it from the store, change it, and then save it again.

But what I think is that what you actually want is to add a new record to the store right?
For that you need to use the 'add' method:
store.put({id: sql_id, customer: sql_company, contact: sql_contact});

Open in new window

Here's some documentation that you'll find useful
0
 
LVL 2

Author Comment

by:detox1978
ID: 40477377
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.
0
 
LVL 2

Author Comment

by:detox1978
ID: 40477389
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.
0
 
LVL 2

Author Closing Comment

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

db.transaction(function (tx) {
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question