I have created a little Phonegap application that gets news data from an Ajax call via XML. This works fine, but I would like to store the data in a database table to also allow offline reading of the news.
So when the Ajax callback loops through the data, I fill a global news object with it and then call a function to check if the data is already stored in the database. If not, it should be inserted into the database news table.
The problem is that the in the transaction to store the news in the table, it seems like my news Object is not present anymore because I get the message:
Uncaught TypeError: Cannot read property 'title' of undefined in ...
So how can I make sure that this work? Here is the code of the part where I select the news and want to check if it's already there:
// Check if a news from the internet already exists in the database; If not, insert it
function checkNewsInDB(){
db.transaction(function(tx){
tx.executeSql("SELECT * FROM NEWS", [], checkSuccess, dbErrorCB);
}, dbErrorCB, dbSuccessCB);
}
// Result Callback from the News Check
function checkSuccess(ctx, result){
var len = result.rows.length;
var found = false;
for(var n = 0; n < newsContainer.length; n++){
for(var r = 0; r < len; r++){
if(result.rows.item(r).n_title == newsContainer[n].title
&& result.rows.item(r).n_pubdate == newsContainer[n].pubdate){
found = r;
}
}
if(found == false){
db.transaction(function(tx){
tx.executeSql("INSERT INTO NEWS (n_title, n_link, n_creator, n_pubdate, n_description) VALUES (?,?,?,?,?)", [newsContainer[n].title, newsContainer[n].link, newsContainer[n].creator, newsContainer[n].pubdate, newsContainer[n].description], insertSuccess, dbErrorCB);
}, dbErrorCB, dbSuccessCB);
} else {
found = false;
}
}
}
The newsContainer IS filled with a few rows of data, I have checked that. I would be very happy if somebody could help me understand why this does not work.
Thanks in advance!
Greetings,
Bernd
I have created a little Phonegap application that gets news data from an Ajax call via XML. This works fine, but I would like to store the data in a database table to also allow offline reading of the news.
So when the Ajax callback loops through the data, I fill a global news object with it and then call a function to check if the data is already stored in the database. If not, it should be inserted into the database news table.
The problem is that the in the transaction to store the news in the table, it seems like my news Object is not present anymore because I get the message:
Uncaught TypeError: Cannot read property 'title' of undefined in ...
So how can I make sure that this work? Here is the code of the part where I select the news and want to check if it's already there:
// Check if a news from the internet already exists in the database; If not, insert it
function checkNewsInDB(){
db.transaction(function(tx){
tx.executeSql("SELECT * FROM NEWS", [], checkSuccess, dbErrorCB);
}, dbErrorCB, dbSuccessCB);
}
// Result Callback from the News Check
function checkSuccess(ctx, result){
var len = result.rows.length;
var found = false;
for(var n = 0; n < newsContainer.length; n++){
for(var r = 0; r < len; r++){
if(result.rows.item(r).n_title == newsContainer[n].title
&& result.rows.item(r).n_pubdate == newsContainer[n].pubdate){
found = r;
}
}
if(found == false){
db.transaction(function(tx){
tx.executeSql("INSERT INTO NEWS (n_title, n_link, n_creator, n_pubdate, n_description) VALUES (?,?,?,?,?)", [newsContainer[n].title, newsContainer[n].link, newsContainer[n].creator, newsContainer[n].pubdate, newsContainer[n].description], insertSuccess, dbErrorCB);
}, dbErrorCB, dbSuccessCB);
} else {
found = false;
}
}
}
The newsContainer IS filled with a few rows of data, I have checked that. I would be very happy if somebody could help me understand why this does not work.
Thanks in advance!
Greetings,
Bernd
Share Improve this question edited Nov 24, 2012 at 14:01 dda 6,2132 gold badges27 silver badges35 bronze badges asked Nov 24, 2012 at 12:52 bbasmerbbasmer 1572 silver badges13 bronze badges2 Answers
Reset to default 4db.transaction is asynchronous - by the time executeSql actually runs, n has already been incremented to the end of the loop.
Rather than creating a new transaction for each item, try moving the loop inside the transaction function.
Thanks for the answer. Here is the code that works for all people who have the same problem:
// Check if a news from the internet already exists in the database; If not, insert it
function checkNewsInDB(){
db.transaction(function(tx){
tx.executeSql("SELECT * FROM NEWS", [], checkSuccess, dbErrorCB);
}, dbErrorCB, dbSuccessCB);
}
// Result Callback from the News Check
function checkSuccess(ctx, result){
var len = result.rows.length;
var found = false;
for(var n = 0; n < newsContainer.length; n++){
for(var r = 0; r < len; r++){
if(result.rows.item(r).n_title == newsContainer[n].title
&& result.rows.item(r).n_pubdate == newsContainer[n].pubdate){
found = r;
}
}
if(found == false){
var title = newsContainer[n].title;
var link = newsContainer[n].link;
var creator = newsContainer[n].creator;
var pubdate = newsContainer[n].pubdate;
var description = newsContainer[n].description;
ctx.executeSql("INSERT INTO NEWS (n_title, n_link, n_creator, n_pubdate, n_description) VALUES (?,?,?,?,?)",
[title, link, creator, pubdate, description], insertSuccess, dbErrorCB);
} else {
found = false;
}
}
}