Thursday 4 May 2017

i=0;i

I have a for loop.

With a rule set that i should only be less than a length, that length is 2. When I console log i inside the for loop, it reaches the length.

How is this possible.... what the..

for (i = 0; i < json.data.length-1; i++) {
                    console.log("this is i %s and this is json.data.length %s", i, json.data.length)
                    // Find all stored posts with the facebook id
                    postModel.find({fbId: json.data[i].id}).then(function(posts){
                        // CHECK IF OUR POST LOOKUP USING FACEBOOK POST ID RETURNED ANY POSTS
                        // console.log(posts)
                        // console.log(i)
                        // console.log("this is i %s and this is json.data.length %s", i, json.data.length)
                        if(posts.length > 0) {

                                // IF THE RECEIVED POST UPDATE TIME DOES NOT MATCH THE STORED POSTS LAST UPDATED TIME WE REPLACE THE DATA AND SAVE THE HISTORY
                            if(posts[0].content.data.updated_time !== json.data[i].updated_time){
                                if(json.data[i].hasOwnProperty("message") && posts[0].hasOwnProperty("message")){
                                    posts[0].history.push(posts[0].content.data)
                                    posts[0].lastUpdated = json.data[i].updated_time
                                    posts[0].message = json.data[i].message
                                    posts[0].save()
                                    if(logging){
                                        console.log("saved post succesfully");
                                    }
                                    // THEN GET THE POSTS UPDATED EDGES
                                    for (var k = 0; k < postEdges.length; k++) {
                                        getPostEdgeAndSave(postEdges[k], json.data[i].id, posts[0]._id, accessToken, postModel)
                                    }
                                }
                                // Otherwise check if the returned data is a story
                                else if(json.data[i].hasOwnProperty("story") && posts[j].hasOwnProperty("story")){
                                    posts[j].history.push(posts[j].content.data)
                                    posts[j].lastUpdated = json.data[i].updated_time
                                    posts[j].story = json.data[i].message
                                    posts[j].save()
                                }
                                // Else if not message, story, or etc.. just return
                                else {
                                    return
                                }
                            }
                            // IF THE LAST TIME THE POST WAS UPDATED IS THE SAME AS THE LAST UPDATED TIME STORED THEN WE DO NOTHING
                            else {
                                return
                            }
                        }
                        // IF WE RECEIVED NO POSTS IN RESPONSE THEN THE POST HASN'T BEEN STORED AND WE STORE IT FOR THE FIRST TIME
                        else if (posts.length == 0) {
                            // MAKE A NEW POST OBJECT
                            console.log("this is i %s and this is json.data.length %s", i, json.data.length)
                            console.log(json.data)
                            console.log(i)
                            if(json.data[i].hasOwnProperty("message") && posts[0].hasOwnProperty("message")){
                                var newPost = new postModel({
                                    message: json.data[i].message,
                                    fbId: json.data[i].id,
                                    lastUpdated: json.data[i].updated_time
                                })
                                // SAVE THE NEW POST
                                console.log("saved post succesfully");
                                newPost.save()
                                // GET ALL THE POSTS EDGES DEFINED BY postEdges Object
                                for (var k = 0; k < postEdges.length; k++) {
                                    getPostEdgeAndSave(postEdges[k], json.data[i].id, newPost._id, accessToken, postModel)
                                }
                            }
                            else if (json.data[i].hasOwnProperty("story") && posts[0].hasOwnProperty("story")){
                                var newPost = new postModel({
                                    story: json.data[i].story,
                                    fbId: json.data[i].id,
                                    lastUpdated: json.data[i].updated_time
                                })
                                // SAVE THE NEW POST
                                console.log("saved post succesfully");
                                newPost.save()
                                // GET ALL THE POSTS EDGES DEFINED BY postEdges Object
                                for (var k = 0; k < postEdges.length; k++) {
                                    getPostEdgeAndSave(postEdges[k], json.data[i].id, newPost._id, accessToken, postModel)
                                }

                            }
                            // Else if not message, story, or etc.. just return
                            else {
                                return
                            }
                        }
                    })
                    // If we are at the end of the returned data list then we request the next set of posts
                    if(i == json.data.length -1) {
                        // requestPosts(json.paging.next, accessToken, postModel)
                    }
                }`

You'll notice my code up above has json.data.length-1 this is the only thing that will get i to only increment to 1 when json.data.length is 2, it is using equals instead of less than...??? How is this possible?



via lopu

No comments:

Post a Comment