简体   繁体   中英

Why is my new object undefined?

I am creating a script to pull certain information from a Facebook stream and insert them into an object I create. I would like the object to look like this:

fbObj {
        {"name": "blah", "post": "this is a post"}
        {"name": "blah_2", "post": "this is another post"}
       }

Is this a good way to create that object?

 <script language="javascript" type="text/javascript">
        function buildObject() {

           //create the object
            var fbObj = {}
            FB.api('me?fields=feed.fields(story,message,from,updated_time)', function(response) {
                console.log(response)

            //loop through feed, add items to object
        for (i = 0, l = response.feed.data.length; i < l; i++){
            var post = response.feed.data[i];
            if (post.story){
                fbObj[i].name = post.from.name
                fbObj[i].post = post.story
                console.log(fbObj[i])
                }
            if (post.message){
                fbObj[i].name = post.from.name
                fbObj[i].post =  post.message
                console.log(fbObj[i])
        }

        var x = document.getElementById("toObject");
            x.innerHTML=fbObj;
        }
        })
        }

fbObj的访问方式像数组,而不是对象,因此应对其进行初始化: var fbObj = []

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM