【问题标题】:Cannot read property 'length' of undefined node.js无法读取未定义 node.js 的属性“长度”
【发布时间】:2014-09-30 04:41:13
【问题描述】:

我有一些 JSON 正在传递到 Node.js,我正在尝试解析它并将其显示在页面上。然而,在 Stack Overflow 上搜索并尝试了各种解决方案(目前阅读this)后,我收到了这个错误作为回报:

TypeError: D:\Programming\node\yale\views\index.jade:10
    8|          //- p= post.comments[1].data
    9|          each comment in post.comments
  > 10|                 each text in comment.data
    11|                     p= text.from.name
    12| 
    13| 

Cannot read property 'length' of undefined
    at $$l (eval at <anonymous> (D:\Programming\node\yale\node_modules\jade\lib\jade.js:172:8), <anonymous>:122:31)
    at $$l (eval at <anonymous> (D:\Programming\node\yale\node_modules\jade\lib\jade.js:172:8), <anonymous>:153:4)
    at eval (eval at <anonymous> (D:\Programming\node\yale\node_modules\jade\lib\jade.js:172:8), <anonymous>:160:4)
    at eval (eval at <anonymous> (D:\Programming\node\yale\node_modules\jade\lib\jade.js:172:8), <anonymous>:301:4)
    at eval (eval at <anonymous> (D:\Programming\node\yale\node_modules\jade\lib\jade.js:172:8), <anonymous>:311: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)

这是我的 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"
                }
            }
        }
    }
]

索引.jade

扩展布局

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

索引.js

var express = require('express');
var router = express.Router();
var fs = require('fs');
var path = require('path');

var file = '../public/sample.json';
var filePath = path.join(__dirname, '../public/sample.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;

我的问题是,如何检索 Comments 对象中的数据?感谢您的帮助!

【问题讨论】:

    标签: json node.js pug


    【解决方案1】:

    我认为您想迭代帖子数据,而不是对象本身。

    each comment in post.comments.data
      p= comment.from.name
    

    【讨论】:

      【解决方案2】:

      comments 不是 JSON 结构中的数组,它是一个对象。试着把它做成一个数组。

      【讨论】:

      • 我该怎么做?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多