Node.js通过大量(3000行)JSON数据进行解析

Node.js通过大量(3000行)JSON数据进行解析,json,node.js,Json,Node.js,我有一个很奇怪的问题。因此,我通过使用Facebook Graph API从Facebook群组检索了一些JSON数据,该API返回了大量数据。我一直试图用Jade解析它来显示它,我让它工作了——然而,只有几篇文章。我把文件缩减到一到两篇文章中,效果很好。但是,当我切换到更大的文件时,它会返回一个未定义的错误。你们知道发生了什么事吗 较小的JSON文件: [ { "id": "362285220481827_740014816042197",

我有一个很奇怪的问题。因此,我通过使用Facebook Graph API从Facebook群组检索了一些JSON数据,该API返回了大量数据。我一直试图用Jade解析它来显示它,我让它工作了——然而,只有几篇文章。我把文件缩减到一到两篇文章中,效果很好。但是,当我切换到更大的文件时,它会返回一个
未定义的
错误。你们知道发生了什么事吗

较小的JSON文件:

 [
        {
            "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"
                    }
                }
            }
        }
    ] 
此处有更大的JSON文件:

以下是我一直在运行的Jade代码:

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
路由文件:

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;
以下是我切换到较大文件时出现的错误:

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)
TypeError:D:\Programming\node\yale\views\index.jade:9
7 | p=发布消息
8 |/-p=post.comments.data
>9 | post.comments.data中的每条评论
10 | p=评论来源
无法读取未定义的属性“数据”
在$$l处(在(D:\Programming\node\yale\node\u modules\jade\lib\jade.js:172:8),:64:28处评估)
评估时(评估时为(D:\Programming\node\yale\node\u modules\jade\lib\jade.js:172:8),:96:4)
评估时(评估时为(D:\Programming\node\yale\node\u modules\jade\lib\jade.js:172:8),:173:4)
评估时(评估时为(D:\Programming\node\yale\node\u modules\jade\lib\jade.js:172:8),:183:21)
在res(D:\Programming\node\yale\node\u modules\jade\lib\jade.js:173:38)
在Object.exports.render(D:\Programming\node\yale\node\u modules\jade\lib\jade.js:269:10)
在Object.exports.renderFile(D:\Programming\node\yale\node\u modules\jade\lib\jade.js:305:18)
在View.exports.renderFile[作为引擎](D:\Programming\node\yale\node\u modules\jade\lib\jade.js:290:21)
在View.render(D:\Programming\node\yale\node\u modules\express\lib\View.js:76:8)
在Function.app.render(D:\Programming\node\yale\node\u modules\express\lib\application.js:503:10)

我已经做了好几个小时了,我一辈子都搞不清楚到底出了什么问题。谢谢你抽出时间

这不是节点问题,而是数据问题

有些帖子,即第20篇没有任何评论,因此它们没有
评论
属性:

{
    "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
}

因此,执行
thatPost.comments
返回
undefined
因此执行
undefined.data
是一个错误。在执行
post.comments.data
之前,您需要检查post是否具有comment属性,在其前面加上
if
检查。

因此,如果我将其粘贴到Heroku或其他服务上,会解决问题吗?我试图在本地运行时出错。这是一个很小的JSON文件-小于1 MB,问题在于您对数据的假设而不是它的大小。@BenjaminGruenbaum-谢谢您的帮助!我是个新手,所以3000行在我眼里仍然是一个很大的数目:)别担心,这些错误发生在我们最好的人身上。重要的是,您已经学会了如何处理“无法读取未定义的”错误的属性“foo:)