我在这里遇到一个非常奇怪的问题。所以我通过使用Facebook Graph API从Facebook组中检索了一些JSON数据,这些数据返回了大量的数据。我一直在尝试解析它以使用Jade显示它并且我让它工作 - 但是,只有几个帖子。我将文件减少到一个或两个帖子,它的工作原理。但是,只要我切换到更大的文件,它就会返回undefined
错误。你们对于发生了什么有任何想法吗?
较小的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文件:http://yale-thread.herokuapp.com/fb.json
这是我一直在运行的翡翠代码:
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)
我已经在这里工作了好几个小时,我不能为我的生活弄清楚什么是错的。谢谢你的时间!
答案 0 :(得分:1)
这不是节点问题,而是数据问题。
有些帖子,即第20个帖子没有任何评论,因此他们没有comment
属性:
{
"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
之前,您需要检查帖子是否有评论属性,并在其前面加上if
支票。