从 Nodejs 调用时我的 API 数据不显示
下面是我从 API 获取数据并显示在服务器上的代码,数据进来但不显示在屏幕上:
const express = require('express');
const router = express.Router();
const data = require('../data');
const peopleData = data.people;
router.get('/', async (req, res) => {
try {
const peopleList = await peopleData.getPeople();
res.render('people/user',{peopleList:peopleList});
} catch (e) {
res.status(500).send();
}
});
module.exports = router;
数据功能:
require("util").inspect.defaultOptions.depth = null;
const axios = require('axios');
async function getPeople(){
const { data } = await axios.get('https://gist.githubusercontent.com/SwayamShah97/0f2cb53ddfae54eceea083d4aa8d0d65/raw/d7d89c672057cf7d33e10e558e001f33a10868b2/people.json');
return data; // this will be the array of people objects
}
module.exports = {
getPeople
}
把手:
<div>
<table>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
{{#each peopleList}}
<tr>
<td>{{id}}</td>
<td>{{firstName}}</td>
<td>{{lastName}}</td>
</tr>
{{/each}}
</table>
</div>
不确定为什么没有显示,我控制台日志并且数据正常。
Below is my code to get data from an API and display on my server, the data comes in but does not display on the screen :
const express = require('express');
const router = express.Router();
const data = require('../data');
const peopleData = data.people;
router.get('/', async (req, res) => {
try {
const peopleList = await peopleData.getPeople();
res.render('people/user',{peopleList:peopleList});
} catch (e) {
res.status(500).send();
}
});
module.exports = router;
Data function:
require("util").inspect.defaultOptions.depth = null;
const axios = require('axios');
async function getPeople(){
const { data } = await axios.get('https://gist.githubusercontent.com/SwayamShah97/0f2cb53ddfae54eceea083d4aa8d0d65/raw/d7d89c672057cf7d33e10e558e001f33a10868b2/people.json');
return data; // this will be the array of people objects
}
module.exports = {
getPeople
}
Handlebars:
<div>
<table>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
{{#each peopleList}}
<tr>
<td>{{id}}</td>
<td>{{firstName}}</td>
<td>{{lastName}}</td>
</tr>
{{/each}}
</table>
</div>
Not sure why nothing displays, I console log and the data comes in fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论