从 Nodejs 调用时我的 API 数据不显示

发布于 2025-01-09 08:39:05 字数 1428 浏览 0 评论 0原文

下面是我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文