无法迭代节点中的json对象,表达应用程序

时间:2016-03-04 14:00:25

标签: json node.js mongodb express mean

这是我的玉码

block content
    .container
        #{data} //prints the whole json doc
        p Here the list of topics will be shown
        for topic,index in data
                p #{index}#{topic.title}

在“此处将显示主题列表”之后显示继续索引,直到1002。但是没有数据与索引一起显示。

这是我的json数据,由#{data}

打印
[{"_id":"56cc8af53ce0def08a1fa4fa","title":"Rotational Motion","subtopics":[{"stitle":"Introduction","versions":[{"desc":"What is rotational motion?"},{"desc":"Describe it?"}]},{"stitle":"Defination","versions":[{"desc":"Define rotational motion"},{"desc":"Define Rotational motion in detail"}]},{"stitle":"Derivation","versions":[{"desc":"Define rotational motion"},{"desc":"Define Rotational motion in detail"}]}]},{"_id":"56cc71107bb2a0733aba0007","title":"Wave Motion","subtopics":[{"stitle":"Introduction","versions":[{"desc":"What is rotational motion?"},{"desc":"Describe it?"}]},{"stitle":"Defination","versions":[{"desc":"Define rotational motion"},{"desc":"Define Rotational motion in detail"}]}]},{"_id":"56c635b38aff62a82b245e20","title":"Sound and Light","subtopics":[{"stitle":"Introduction","versions":[{"desc":"What is rotational motion?"},{"desc":"Describe it?"}]},{"stitle":"Defination","versions":[{"desc":"Define rotational motion"},{"desc":"Define Rotational motion in detail"}]}]}]

这就是我渲染文件的方式 -

router.get('/', function (req, res) {
    var options = {
      host: 'someurl',
      port: 80,
      path: '/topics'
    };

    http.get(options, function(webresponse) {
      console.log("Got response: " + webresponse.statusCode);

      webresponse.on("data", function(chunk) {
        console.log("data: " + chunk);
        var data = chunk; 
        console.log(data); //prints the json data correctly
        //rendering html with params
        res.render('index', { user : req.user , data : data });

      });
    }).on('error', function(e) {
      console.log("Got error: " + e.message);
    });
});

1 个答案:

答案 0 :(得分:4)

Jade使用each而非for。将代码更新为each topic in data,它应该可以正常工作。

Jade Docs

编辑:按照服务器代码,似乎data只是一个以字符串形式返回的JSON对象,这就是为什么你无法正确循环它的原因。在将JSON.parse(data)发送给渲染器之前使用$columns= (Get-Content $csvfile -First 1).Split($csvdelimiter) 应解决该问题