Andrew
Andrew

Reputation: 3589

Node.js parsing through a large(3000+ lines) of JSON data

I have a pretty strange problem going on here. So I retrieved some JSON data from a Facebook group through the use the of the Facebook Graph API, which returned me a massive amount of data. I've been trying to parse through it to display it using Jade and I got it to work - however, only for a couple of posts. I cut down the file to one or two posts, and it works. However, as soon as I switch out to the bigger file, it returns me an undefined error. Do you guys have any ideas about what's going on?

Smaller JSON file:

 [
        {
            "id": "362285220481827_740014816042197",
            "from": {
                "id": "1677018201",
                "name": "ABC"
            },
            "message": "Yo!",


            "comments": {
                "data": [
                    {
                        "id": "740018622708483",
                        "from": {
                            "id": "100003969443486",
                            "name": "XTZ"
                        },
                        "message": "Hey!",
                        "can_remove": false,
                        "created_time": "2014-06-14T17:18:48+0000",
                        "like_count": 0,
                        "user_likes": false
                    }
                ],
                "paging": {
                    "cursors": {
                        "after": "NzQ0MTMxNTc1NjMwNTIx",
                        "before": "NzQwMDE4NjIyNzA4NDgz"
                    }
                }
            }
        }
    ] 

Larger JSON file here: http://yale-thread.herokuapp.com/fb.json

Here's the Jade code I've been running:

extends layout

block content
    each post in datas 
        div(id='post')
            p= post.from.name
            p= post.message
            //- p= post.comments.data
            each comment in post.comments.data
                p= comment.from

Routes file:

var express = require('express');
var router = express.Router();
var fs = require('fs');
var path = require('path');
var filePath = path.join(__dirname, '../public/fb.json');
    /* GET home page. */
router.get('/', function(req, res) {
    fs.readFile(filePath, 'utf8', function(err, data) {
        if(err) {
            console.log(err);
        } else {
            data = JSON.parse(data);
            res.render('index', { datas: data });
            console.log(data);
        }

    });

});

module.exports = router;

Here's the error I get as soon as I switch out to the larger file:

TypeError: D:\Programming\node\yale\views\index.jade:9
    7|          p= post.message
    8|          //- p= post.comments.data
  > 9|          each comment in post.comments.data
    10|                 p= comment.from

Cannot read property 'data' of undefined
    at $$l (eval at <anonymous> (D:\Programming\node\yale\node_modules\jade\lib\jade.js:172:8), <anonymous>:64:28)
    at eval (eval at <anonymous> (D:\Programming\node\yale\node_modules\jade\lib\jade.js:172:8), <anonymous>:96:4)
    at eval (eval at <anonymous> (D:\Programming\node\yale\node_modules\jade\lib\jade.js:172:8), <anonymous>:173:4)
    at eval (eval at <anonymous> (D:\Programming\node\yale\node_modules\jade\lib\jade.js:172:8), <anonymous>:183:21)
    at res (D:\Programming\node\yale\node_modules\jade\lib\jade.js:173:38)
    at Object.exports.render (D:\Programming\node\yale\node_modules\jade\lib\jade.js:269:10)
    at Object.exports.renderFile (D:\Programming\node\yale\node_modules\jade\lib\jade.js:305:18)
    at View.exports.renderFile [as engine] (D:\Programming\node\yale\node_modules\jade\lib\jade.js:290:21)
    at View.render (D:\Programming\node\yale\node_modules\express\lib\view.js:76:8)
    at Function.app.render (D:\Programming\node\yale\node_modules\express\lib\application.js:503:10)

I've been at this for hours and I can't for the life of me figure out what's wrong. Thank you for your time!

Upvotes: 0

Views: 124

Answers (1)

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276306

This is not a node problem but rather a data problem.

Some posts, namely the 20th one do not have any comments so the they don't have a comment property:

{
    "id": "362285220481827_742250359151976",
    "from": {...},
    "to": {...}
    },
    "message": "Hey internationals! Have you signed up for OIS yet?...",
    "picture": "https:...",
    "name": "OIS 2014",
    "description": "Welcome to the official OIS ...",
    "icon": "...",
    "actions": [...],
    "privacy": {...},
    "type": "link",
    "created_time": "2014-06-19T11:35:06+0000",
    "updated_time": "2014-06-19T11:35:06+0000",
    "likes": {...}
    }
    // no comment property
}

For this reason, doing thatPost.comments returns undefined so doing undefined.data is an error. You need to check if the post has a comment property before doing post.comments.data, prefix it with an if check.

Upvotes: 1

Related Questions