Solved

run code within JSON **URGENT**

Posted on 2014-12-02
4
384 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

770 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