Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Please Help me,

i have pasted my full js code here..


$(function ()
{
    //alert('Im Ready');
    var db = openDatabase('Test', '1.0', 'Test', 2 * 1024 * 1024);
    db.transaction(function (tx)
    {
        tx.executeSql('drop table countries ');
        tx.executeSql('CREATE TABLE IF NOT EXISTS countries (id INTEGER PRIMARY KEY AUTOINCREMENT,countryId,countryname)');
        //alert('table created');
    });
    main();
});

function main() {
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        }
        else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        var url = "http://ws.geonames.org/searchJSON";

        xmlhttp.open("GET", url, true);
        xmlhttp.send();

        xmlhttp.onreadystatechange = function () {

            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                jsondata = xmlhttp.responseText;
                var json = eval('(' + jsondata + ')');

                pushtoDB(json);
            }
        }
}

function pushtoDB(jsondata) {

    var db = openDatabase('Test', '1.0', 'Test', 2 * 1024 * 1024);
    var i = 0;

     db.transaction(function (tx) {

        for ( i = 0; i <= jsondata.geonames.length; i++) {

var cid = jsondata.geonames[i].countryId; // Error:Cannot read property 'countryId'of undefined)
            var name = jsondata.geonames[i].lng;

            tx.executeSql('INSERT  INTO countries (countryId,countryname) VALUES ("' + cid + '","' + name + '")');
        }
        
    });
}
Posted
Updated 28-Aug-13 20:04pm
v5
Comments
ZurdoDev 28-Aug-13 10:49am    
Where's the problem? Why so much code? Narrow it down to where the issue is.

1 solution

Try:
JavaScript
 for ( i = 0; i < jsondata.geonames.length; i++) {
 
var cid = jsondata.geonames[i].countryId; // Error:Cannot read property 'countryId'of undefined)
            var name = jsondata.geonames[i].lng;
 
            tx.executeSql('INSERT  INTO countries (countryId,countryname) VALUES ("' + cid + '","' + name + '")');
 

        }

Change the <= into <

Hope this helps.
 
Share this answer
 
Comments
Ramkumar K 29-Aug-13 2:06am    
ProgramFOX,Thanks for your help...it worked :)
Thomas Daniels 29-Aug-13 2:31am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900