如何渲染渲染数据节点,ejs和表达?

时间:2016-02-11 13:10:56

标签: node.js sockets express

function (err,result,body) {
    //console.log(typeof body);
    var body = JSON.parse(body);
    //console.log(body);
    var allusers = new Array();
    allusers.push(body);
 }

在body变量中我有json对象,我想在contact.ejs页面上呈现

正文包含以下数据:

{
    "allUsers": [{
        "id": "35",
        "isDevice": "true",
        "username": "IPG06",
        "email": "ldm@email.com",
        "imgLastUpdate": "",
        "name": "[ Big Drum ]",
        "surname": "",
        "jobPositionName": "[ LINEA DE PRODUCCION ]",
        "jobPositionID": "35",
        "identifier": "IPG06"
    }, {
        "id": "37",
        "isDevice": "true",
        "username": "IPG08",
        "email": "ldm@email.com",
        "imgLastUpdate": "",
        "name": "[ Pasto 1 ]",
        "surname": "",
        "jobPositionName": "[ LINEA DE PRODUCCION ]",
        "jobPositionID": "35",
        "identifier": "IPG08"
    }]
}

我需要将以下内容发送到contact.ejs但没有{"allUsers": 它应该以

开头
[{
    "id": "35",
    "isDevice": "true",
    "username": "IPG06",
    "email": "ldm@email.com",
    "imgLastUpdate": "",
    "name": "[ Big Drum ]",
    "surname": "",
    "jobPositionName": "[ LINEA DE PRODUCCION ]",
    "jobPositionID": "35",
    "identifier": "IPG06"
 }, {
    "id": "37",
    "isDevice": "true",
    "username": "IPG08",
    "email": "ldm@email.com",
    "imgLastUpdate": "",
    "name": "[ Pasto 1 ]",
    "surname": "",
    "jobPositionName": "[ LINEA DE PRODUCCION ]",
    "jobPositionID": "35",
    "identifier": "IPG08"
 }]


res.render('pages/contacts',{
    ids:contacts_array,
    allusers:allusers
});

1 个答案:

答案 0 :(得分:0)

答案很简单:
你有 body 变量,其属性'allUsers'的值为ojects数组。

所以,'body.allUsers'就是你需要发送的全部内容。

您的代码就像:

res.render('pages/contacts',{
    ids:contacts_array,
    allusers:body.allUsers // This will send array of objects.
});