How to send json data to jade from node.js?

落爺英雄遲暮 提交于 2019-12-12 03:25:30

问题


i have really important question about jade and node.js usage. i have a userlist function in node.js. i wanna show my json data in side of node.js as jade page. but it will be empty.

i have 2 file: USERLIST.jade and index.js

index.js:


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

/* GET home page. */
router.get('/', function (req, res) {
    res.render('index', { title: 'Express' });
});


/* GET Userlist page. */
router.get('/userlist', function (req, res) {
    var json = { name: "yusuf karatoprak", age: 42 };
    res.render('userlist', { userlist : json });

});


module.exports = router;


USERLIST.jade



extends layout

block content
    h1.
        User List
    ul
        each user, i in userlist
            li

              #{user.name}

but it creates empty page. i can not see my json data...


回答1:


Try this...

extends layout

block content
    h1.
        User List
    ul
        li= userlist.name



回答2:


You're passing a JavaScript object, not an array. You don't need to loop it.

Either change your data to:

var json = [{ name: "yusuf karatoprak", age: 42 }];

Or change your view to:

extends layout

block content
    h1.
        User List
    ul
        li
            #{userlist.name}



回答3:


This must work-

extends layout    
block content    
    h1.    
        User List    
    ul    
        each user, i in userlist    
            li #{user}


来源:https://stackoverflow.com/questions/27966173/how-to-send-json-data-to-jade-from-node-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!