0
var url = "http://api.reddit.com/";

var dataArray = [];
var working = function(){
var getData = JSON.parse(this.responseText); 
var titles = getData.data.children;

for(var i=0, j=titles.length; i<j; i++)
{
    var title = titles[i].data.title;
    dataArray.push({
        title: title,
        favorite: 0
    });
    }

    save(dataArray);

}; //working


var save = function(arg){
    console.log(arg);
    var db = Ti.Database.open("newData");
db.execute('CREATE TABLE IF NOT EXISTS redditTitles (id INTEGER PRIMARY KEY, name       TEXT, favorite INTEGER)');
db.execute('INSERT INTO redditTitles (name, favorite) VALUES (?, ?)',  arg.title, arg.favorite);
var rowID = db.lastInsertRowId;
//newRow.id = rowID;
//rows.close();
db.close(); 
gather();
};

var dataContent = [];

var gather = function(){    

var db = Ti.Database.open("newData");
var dbRows = db.execute("SELECT name, favorite FROM redditTitles"); // Returns a Result Set object
while(dbRows.isValidRow()){
    dataContent.push({
        title: dbRows.fieldByName("name"),
        fav: dbRows.fieldByName("favorite")

    });
    console.log("dataContent: "+ dataContent.title);
    dbRows.next();
}
dbRows.close();
db.close();
console.log(dataContent);
userInterAPI();

};

var error = function(){

alert("Please check your network connection and try again.");

};


var client = Ti.Network.createHTTPClient({
onload: working,
onerror: error,
timeout: 5000


});


client.open("GET", url);

client.send();

So Basically me and my instructor have been scratching our heads trying to figure out why the arg will show all of the data but after the data is saved and we go to re console log it out, it will show up as null. Not sure why. Someone please help me!

1 Answer 1

1

You are saving just one item (Incorrectly - that's why is undefined). If you want to save everything you have to iterate through whole array.

var save = function(arg) {
    console.log(arg);
    var db = Ti.Database.open("newData");

    db.execute('CREATE TABLE IF NOT EXISTS redditTitles (id INTEGER PRIMARY KEY, name       TEXT, favorite INTEGER)');
    db.execute("BEGIN"); // Transaction

    arg.forEach(function(item) {
        db.execute('INSERT INTO redditTitles (name, favorite) VALUES (?, ?)',  item.title, item.favorite);
        //var rowID = db.lastInsertRowId;
    });

    db.execute("COMMIT");

    db.close(); 
    gather();
};

In the function called gather - if you want to see selected title you should use:

console.log(dbRows.fieldByName("name"))

alternatively (This is what you wanted to use):

console.log(dataContent[dataContent.length - 1].title)

instead of

console.log(dataContent.title); // dataContent is an Array.

*Of course you better avoid using dataContent.length in every iteration. That's just an example.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.