0

I am trying to create a dynamic array and push elements to it using Javascript. Being a beginner, I am having problems with it.

P.S. I am fetching the values all fine (checked with console.log), the only problem is that I am unable to put it in the database.

Below is my code -

 (function makelist() {
    var listHeaderData =[];
    var listChildData = [];
    var listImagePath = [];
    var nextPageRefLink = ['imageSlider.html','NewsItem_descp_1.html','http://www.yahoo.com'];
    Parse.initialize("XN0sv3fBqB380fCpwohuHAgansghkiDp8MhbyJDs", "xzoETxUUxicHAtdwqbZh65XtTAFaona3a6cC9jSB");
    console.log("Parse Initailized inside javascript");
    var newsFeed = Parse.Object.extend("NewsFeed");
    var query = new Parse.Query(newsFeed);
    query.find ({
        success: function(results) {
            var output ="";
            for(var  i in results) {
                //listHeaderData DATA
                var heading = results[i].get("Heading");
                listHeaderData.push(heading);
                console.log("Header : "+heading);
                //listChildData DATA
                var description = results[i].get("description");
                listChildData.push(description);
                console.log("Description : "+description);
                if(results[i].get("Image")) {
                    var file = results[i].get("Image");
                    //listImagePath URL's
                    var url = file.url();
                    listImagePath.push(url);
                    console.log("Image URL is:"+url);
                }
                console.log("Heading:"+heading);
                console.log("Description:"+description);
            }
        } , error: function(error){
            console.log("Query error :"+error.message);
        }
    }); 
}

The problem is nothing is getting added to the arrays that I have defined above. I am using .push() function for it. What can be the problem ?

6
  • You would be better off using for(var i=0; i < results.length; i += 1) for a start as you don't want to be checking all the properties on the results object, just the array values. Commented Nov 3, 2015 at 12:32
  • When and/or When you had the problem? What precisely problem? Commented Nov 3, 2015 at 12:33
  • @frikinside I have corrected my post. The problem is nothing is getting added to the arrays that I have defined above. I am using .push() function for it. Commented Nov 3, 2015 at 12:44
  • is the console.log(url) logging the right data? Commented Nov 3, 2015 at 12:47
  • @JosephKhella Yes it is... Commented Nov 3, 2015 at 12:50

1 Answer 1

1

Where do you use these list* variables?

If you try to access listHeaderData right after query.find() then you will never get the data as it will be available later, after query.find request is finished. This is why you need to check these arrays right after the for-loop within success callback.

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

4 Comments

No that isn't the problem. I have correct my post. The problem is that nothing is getting added to the array via the .push() function.
Your list* variables exist within makelist() function scope, populated asynchronously after the response arrives in query.find() call. Could you tell me where you use these list* variables?
Right...Below this code I have more code written that dynamically creates html elements through the same script. Like this : var spanTag = document.createElement("span"); spanTag.setAttribute("class", "title"); spanTag.innerHTML = listHeaderData[i];
This is the problem. Put this code after the for-loop. for (var i in results) { /*...*/ }; var spanTag...

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.