【问题标题】:forEach Looping of JSON.stringify data from API using ejs使用 ejs 从 API 循环 JSON.stringify 数据的 forEach
【发布时间】:2020-01-21 17:31:33
【问题描述】:

这是一个 node.js 应用程序,使用 express 和 ejs 使用 API 调用数据,然后使用 JSON.stringify 在页面上显示该数据。我试图通过 ejs 页面上的 forEach 循环显示所有 API 数据调用。我可以使数据单独显示,但我不能通过 forEach 循环使其显示尽可能多。

这是可行的,我在 server.js 文件中设置了以下 api:

server.js

//API v4/user/beers/username
const untappdAPI = { method: 'GET',
  url: 'https://api.untappd.com/v4/user/beers/username',
  qs: 
   { access_token: 'abc123',
     }    
  };

//untappd.ejs page 
app.get('/untappd', function(req, res) {
  try {
    request(untappdAPI, function (error, response, body) {
    if (error) throw new Error(error);
    const info = JSON.parse(body);
    res.render('untappd', {info});
  });
} catch(e) {
  console.log("Something went wrong", e)
}
});

我的 untappd.ejs 文件中有以下内容:

untappd.ejs

<%= JSON.stringify(info.response.beers.items[0].beer.beer_name);%>

当我运行它时,页面上的输出显示 1 啤酒名称:

Beer Name

这是行不通的,现在我想在此页面上显示所有啤酒名称,因此我尝试像这样设置一个 forEach 循环:

<% untappd.forEach(function(info){ %>
      <%= JSON.stringify(info.response.beers.items[0].beer.beer_name);%>
<% }) %>

但是当我运行它时,我收到一条错误消息,提示“未定义 untappd”。我究竟做错了什么?非常感谢您提供的任何帮助!

完整的 JSON

    "meta": {
        "code": 200,
        "response_time": {
            "time": 0.03,
            "measure": "seconds"
        },
        "init_time": {
            "time": 0,
            "measure": "seconds"
        }
    },
    "notifications": {
        "type": "notifications",
        "unread_count": {
            "comments": 0,
            "toasts": 0,
            "friends": 0,
            "messages": 0,
            "venues": 0,
            "veunes": 0,
            "others": 0,
            "news": 5
        }
    },
    "response": {
        "total_count": 680,
        "dates": {
            "first_checkin_date": "Sat, 15 Aug 2015 01:12:58 +0000",
            "start_date": false,
            "end_date": false,
            "tzOffset": "0"
        },
        "is_search": false,
        "sort": false,
        "type_id": false,
        "country_id": false,
        "brewery_id": false,
        "rating_score": false,
        "region_id": false,
        "container_id": false,
        "is_multi_type": false,
        "beers": {
            "count": 2,
            "items": [
                {
                    "first_checkin_id": 806418145,
                    "first_created_at": "Wed, 10 Sep 2020 15:16:51 -0700",
                    "recent_checkin_id": 806418145,
                    "recent_created_at": "Wed, 10 Sep 2020 15:16:51 -0700",
                    "recent_created_at_timezone": "-7",
                    "rating_score": 1.5,
                    "user_auth_rating_score": 1.5,
                    "first_had": "Wed, 10 Sep 2020 15:16:51 -0700",
                    "count": 1,
                    "beer": {
                        "bid": 3419450,
                        "beer_name": "Beer Name",

【问题讨论】:

  • 请向我们展示您的 JSON。如果我们没有看到足够的信息,我们不知道要循环哪个元素。您似乎想要循环 info.response.beers.items 数组,而不是 info 对象,因为它不是数组。实际上,您正在执行untappd.forEach 并且错误非常明确:untappd 不存在。你期望它是什么?我建议您学习一些有关数组及其方法的知识,因为您似乎不知道自己在做什么,只是在那里复制粘贴代码。
  • 感谢 Jorge,我已按要求添加了 JSON
  • 使用这个res.render('untappd', {info});,您可以在untappd ejs 文件中渲染info 对象。你不能循环untappd.forEach(function(info)。这是一个文件
  • 谢谢 Dimitris,那我应该怎么做呢,或者你能指点我一个可以了解这个的地方吗?
  • 我猜你没有听从我的建议 xD

标签: node.js json api express ejs


【解决方案1】:

最后,我需要用一个新命名的函数传递“信息”,这是工作代码...

<% info.forEach(function(beerlist){ %> 
    <%= JSON.stringify(beerlist.response.beers.items[0].beer.beer_name);%> 
<% }) %>

【讨论】:

    猜你喜欢
    • 2020-08-29
    • 2019-01-02
    • 2020-07-26
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 2011-08-23
    相关资源
    最近更新 更多