Solved

run code within JSON **URGENT**

Posted on 2014-12-02
4
372 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now