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
Solved

run code within JSON **URGENT**

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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)
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…

861 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