简体   繁体   中英

Passing data to handlebars - node.js

I am new to node.js , so basically I am making a get request in my router(index.js) and then after I get the result (I could see that I obtained the right result by dumping it in console.log) I am parsing it into a json object and passing it to a render function so that I could use it there, however, I am getting errors. I will post the code segments and the JSON fragment below . Is this the right way to use handlebars?

Router (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 Object

{
    "@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>

I need to go through all user details, but for test purpose I was trying to access the display name of first user returned but I am getting this error:

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)

First you need to render your node.js result to your handlebars template like this:

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

Once you have "all_users" availale you can get the value like this:

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

         </tr>

 {{/each}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM