나는 node.js를 처음 사용하므로 라우터 (index.js)에서 요청을 받고 기본적으로 결과를 얻은 후에 console.log에 덤핑하여 올바른 결과를 얻습니다.) json 객체로 파싱하고 렌더링 함수에 전달하여 거기에서 사용할 수 있도록하지만 오류가 발생합니다. 아래에 코드 세그먼트와 JSON 조각을 게시합니다. 이것이 핸들 바를 사용하는 올바른 방법입니까?핸들 바에 데이터 전달하기 - node.js
라우터 (하는 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": "[email protected]",
"mobilePhone": null,
"officeLocation": null,
"preferredLanguage": null,
"surname": "DEF",
"userPrincipalName": "[email protected]",
"id": "abcd1"
}, {
"businessPhones": [],
"displayName": "ABC XYZ",
"givenName": "ABC",
"jobTitle": "Shipping Clerk",
"mail": "[email protected]",
"mobilePhone": null,
"officeLocation": "Stockton",
"preferredLanguage": null,
"surname": "XYZ",
"userPrincipalName": "[email protected]",
"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)
이 당신이 'display_all_users', {ALL_USERS (이'res.render을 시도해보십시오
당신이이 같은 값을 얻을 수 있습니다 availale "ALL_USERS"가되면 : all_users}); – Gintoki
예. 시도했지만 여전히 동일한 오류가 발생합니다. –
나는 res.render ('display_all_users', {data : all_users})도 시도해 보았습니다. data.value [0] .displayName 같이 핸들 바에 접근하려고 시도했지만 여전히 동일한 결과가 나타납니다. –