将数据传递给车把 - node.js

时间:2016-06-20 17:19:39

标签: javascript json node.js

我是node.js的新手,所以基本上我在我的路由器(index.js)中发出get请求然后在得到结果后(我可以看到我通过将其转储到控制台中获得了正确的结果)。我正在将它解析为一个json对象并将其传递给一个渲染函数,以便我可以在那里使用它,但是,我收到错误。我将在下面发布代码段和JSON片段。这是使用把手的正确方法吗?

路由器(index.js)

router.get('/getusers', function(req, res) {
    wrapRequestAsCallback(req.cookies.TOKEN_CACHE_KEY, {

        onSuccess: function(token) {
            var all_users = {};
            // get all the users
            requestUtil.getJson(
                'graph.microsoft.com',
                '/v1.0/users',
                token.accessToken,
                function(result) {
                    if (result !== null) {
                        console.log(result);
                        all_users = JSON.parse(result);
                        req.session.all_users = all_users;
                        if (all_users !== null) {
                            res.render('display_all_users', all_users);
                        }
                    }
                }
            );
        },

        onFailure: function(err) {
            res.status(err.code);
            console.log(err.message);
            res.send();
        }
    });
});

JSON对象

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users",
    "@odata.nextLink": "https://graph.microsoft.com/v1.0/users?$skiptoken=X%27445370740200010000001B3A4150432E4167656E744074686577696E6567726F7572D303037332D343865352D396432332D633438383336613833636361B900000000000000000000%27",
    "value": [{
            "businessPhones": [],
            "displayName": "ABC DEF",
            "givenName": "ABC",
            "jobTitle": null,
            "mail": "Abc.Def@somemail.com",
            "mobilePhone": null,
            "officeLocation": null,
            "preferredLanguage": null,
            "surname": "DEF",
            "userPrincipalName": "abc.def@somemail.com",
            "id": "abcd1"
        }, {
            "businessPhones": [],
            "displayName": "ABC XYZ",
            "givenName": "ABC",
            "jobTitle": "Shipping Clerk",
            "mail": "Abc.Xyz@somemail.com",
            "mobilePhone": null,
            "officeLocation": "Stockton",
            "preferredLanguage": null,
            "surname": "XYZ",
            "userPrincipalName": "abc.xyz@somemail.com",
            "id": "abcd2"
        }
        .....
    }] }

display_all_users.hbs

<h2 class="ms-font-xxl ms-fontWeight-semibold"> EMPLOYEE DETAILS: </h2>
<div class="ms-TextField">
    <p> {{all_users.value[0].displayName}} </p>
</div>

我需要查看所有用户详细信息,但出于测试目的,我试图访问返回的第一个用户的显示名称,但是我收到此错误:

Error: C:\test\views\display_all_users.hbs: Parse error on line 15:
...    <p> {{all_users.value[0].displayName
-----------------------^

    Expecting 'ID', got 'INVALID'
    at Object.parseError(C: \test\ node_modules\ hbs\ node_modules\ handlebars\ dist\ cjs\ handlebars\ compiler\ parser.js: 150: 11)
    at Object.parse(C: \test\ node_modules\ hbs\ node_modules\ handlebars\ dist\ cjs\ handlebars\ compiler\ parser.js: 202: 22)
    at HandlebarsEnvironment.parse(C: \test\ node_modules\ hbs\ node_modules\ handlebars\ dist\ cjs\ handlebars\ compiler\ base.js: 25: 30)
    at compileInput(C: \test\ node_modules\ hbs\ node_modules\ handlebars\ dist\ cjs\ handlebars\ compiler\ compiler.js: 451: 19)
    at ret(C: \test\ node_modules\ hbs\ node_modules\ handlebars\ dist\ cjs\ handlebars\ compiler\ compiler.js: 460: 18)
    at C: \test\ node_modules\ hbs\ lib\ hbs.js: 63: 19
    at FSReqWrap.readFileAfterClose[as oncomplete](fs.js: 380: 3)

1 个答案:

答案 0 :(得分:0)

首先,您需要将node.js结果渲染到您的手柄模板,如下所示:

res.render('display_all_users', {all_users: all_users});

一旦你拥有&#34; all_users&#34;你可以得到这样的价值:

{{#each all_users}}
        <tr>
              <td headers="name">{{all_users.value[0].displayName}}</td>

         </tr>

 {{/each}}