使用 express 节点将 API 数据渲染到 html/ handlebars
Render API data with express node to html/ handlebars
我需要帮助来渲染从 API 到 html/handlebars 的数据。
我对如何在页面上显示数据有点困惑
这是我到目前为止得到的:
路线 FOLDER/FILE:
const express = require('express');
const router = express.Router();
const us_states = require('../us_state.js');
const fetch = require('node-fetch');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Find My Election', states: us_states });
});
/* GET Election List. */
router.post('/upcomingelections', function(req, res, next) {
fetch(`https://api.turbovote.org/elections/upcoming?district-divisions=ocd-division/country:us/state:ma,ocd-division/country:us/state:ma/place:wayland
`, {
method: 'get',
headers: { 'Accept': 'application/json' },
})
.then(res => res.json())
.then(json => console.log(json));
res.render('electionlist');
});
module.exports = router;
到目前为止,我已经发出了获取请求并存储了数据。然后我使用 res.send 将数据发送到要呈现的车把页面。
None 我想要的数据在页面上看到了。我不知道我做错了什么。
HTML/HANDLEBARS 文件
:
<div class="resultcontainer">
<h1 class="resultTitle"> UPCOMING ELECTION(S)</h1>
<div id="wrapper">
<table id="keywords" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th><span>Description</span></th>
<th><span>Date</span></th>
<th><span>Registration Deadline</span></th>
<th><span>Election Level</span></th>
<th><span>Website</span></th>
</tr>
</thead>
{{#if json}}
<tbody>
<tr>
<td class="lalign"></td>
<td>{{{json.description}}}</td>
<td>{{{json.date}}}}</td>
<td>{{{json.district-divisions[0]['election-authority-level']}}}</td>
<a href={{{json.website}}}>link</a>
</tr>
</tbody>
{{else}}
<p class="empty">No upcoming election</p>
{{/if}}
</div>
</div>
您需要等到 fetch
完成后再渲染。现在您无需等待即可进行渲染,并且不会将任何数据传递给 res.render
此外,您应该始终处理错误。添加一个 .catch
到承诺链,这样如果请求失败你可以结束请求。
router.post('/upcomingelections', function(req, res, next) {
fetch(`https://api.turbovote.org/elections/upcoming?district-divisions=ocd-division/country:us/state:ma,ocd-division/country:us/state:ma/place:wayland`, {
method: 'get',
headers: {
'Accept': 'application/json'
},
})
.then(res => res.json())
.then(json => {
console.log(json);
res.render('electionlist', { json });
})
.catch(err => res.status(500).send(e.message));
});
我需要帮助来渲染从 API 到 html/handlebars 的数据。
我对如何在页面上显示数据有点困惑
这是我到目前为止得到的:
路线 FOLDER/FILE:
const express = require('express');
const router = express.Router();
const us_states = require('../us_state.js');
const fetch = require('node-fetch');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Find My Election', states: us_states });
});
/* GET Election List. */
router.post('/upcomingelections', function(req, res, next) {
fetch(`https://api.turbovote.org/elections/upcoming?district-divisions=ocd-division/country:us/state:ma,ocd-division/country:us/state:ma/place:wayland
`, {
method: 'get',
headers: { 'Accept': 'application/json' },
})
.then(res => res.json())
.then(json => console.log(json));
res.render('electionlist');
});
module.exports = router;
到目前为止,我已经发出了获取请求并存储了数据。然后我使用 res.send 将数据发送到要呈现的车把页面。 None 我想要的数据在页面上看到了。我不知道我做错了什么。 HTML/HANDLEBARS 文件 :
<div class="resultcontainer">
<h1 class="resultTitle"> UPCOMING ELECTION(S)</h1>
<div id="wrapper">
<table id="keywords" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th><span>Description</span></th>
<th><span>Date</span></th>
<th><span>Registration Deadline</span></th>
<th><span>Election Level</span></th>
<th><span>Website</span></th>
</tr>
</thead>
{{#if json}}
<tbody>
<tr>
<td class="lalign"></td>
<td>{{{json.description}}}</td>
<td>{{{json.date}}}}</td>
<td>{{{json.district-divisions[0]['election-authority-level']}}}</td>
<a href={{{json.website}}}>link</a>
</tr>
</tbody>
{{else}}
<p class="empty">No upcoming election</p>
{{/if}}
</div>
</div>
您需要等到 fetch
完成后再渲染。现在您无需等待即可进行渲染,并且不会将任何数据传递给 res.render
此外,您应该始终处理错误。添加一个 .catch
到承诺链,这样如果请求失败你可以结束请求。
router.post('/upcomingelections', function(req, res, next) {
fetch(`https://api.turbovote.org/elections/upcoming?district-divisions=ocd-division/country:us/state:ma,ocd-division/country:us/state:ma/place:wayland`, {
method: 'get',
headers: {
'Accept': 'application/json'
},
})
.then(res => res.json())
.then(json => {
console.log(json);
res.render('electionlist', { json });
})
.catch(err => res.status(500).send(e.message));
});