ejs issue "data.result.forEach is not a function

huangapple go评论59阅读模式
英文:

ejs issue "data.result.forEach is not a function

问题

以下是您要翻译的内容:

我有一个results.ejs网站,我尝试获取api回调的结果。在ejs中,我有以下内容:

    <h1>Results Page!!</h1>
    
    <%data["result"].forEach(function(data){%>
    <li><%= data["address"]%></li>
    <% }) %>

这里的值应该从包含api密钥的app.js中获取,它可以解析body等等:

    var express = require("express");
    var app = express();
    var request = require("request");
    app.set("view engine", "ejs");
    
    var KEY = process.env.token;
    
    var menuSearch = {
      method: "GET",
      url: "https://us-restaurant-menus.p.rapidapi.com/restaurants/zip_code/61603",
      // url: "https://us-restaurant-menus.p.rapidapi.com/restaurants/search/portillos",
      qs: { page: "1" },
      headers: {
        "X-RapidAPI-Key": KEY,
        "X-RapidAPI-Host": "us-restaurant-menus.p.rapidapi.com"
      }
    };
    
    app.get("/results", function(req, res) {
      request(menuSearch, function(error, response, body) {
        // console.log(body);
    
        if (!error && response.statusCode == 200) {
          var data = JSON.parse(body);
          // res.send(data)
          // res.send(data["result"]["data"][0]["address"]["city"])
          res.render("results", { data: data });
        }
      });
      // res.send("Hello, it works!");
    });
    
    var port = process.env.PORT || 3000;
    
    app.listen(port, process.env.IP, function() {
      console.log("running on port: " + port);
    });

现在在最后输出给我:

        1| <h1>Results Page!!</h1>
        2| 
     >> 3| <%data["result"].forEach(function(data){%>
        4| <li><%= data["address"]%></li>
        5| <% }) %>
        6| 
    
    data.result.forEach is not a function...

有人可以告诉我它断开在哪里吗?

我认为密钥应该在URL本身中,尽管密钥已经在标头中。我不太确定。

希望这可以帮助您理解问题所在。如果您需要更多帮助,请随时告诉我。

英文:

I have a results.ejs site that I try to get the results of an api callback. In the ejs I have the following:

<h1>Results Page!!</h1>

<%data["result"].forEach(function(data){%>
<li><%= data["address"]%></li>
<% }) %>

Here the value is supposed to be taken from app.js that includes the api key its which works just parsing the body etc:

var express = require("express");
var app = express();
var request = require("request");
app.set("view engine", "ejs");

var KEY = process.env.token;

var menuSearch = {
  method: "GET",
  url: "https://us-restaurant-menus.p.rapidapi.com/restaurants/zip_code/61603",
  // url: "https://us-restaurant-menus.p.rapidapi.com/restaurants/search/portillos",
  qs: { page: "1" },
  headers: {
    "X-RapidAPI-Key": KEY,
    "X-RapidAPI-Host": "us-restaurant-menus.p.rapidapi.com"
  }
};

app.get("/results", function(req, res) {
  request(menuSearch, function(error, response, body) {
    // console.log(body);

    if (!error && response.statusCode == 200) {
      var data = JSON.parse(body);
      // res.send(data)
      // res.send(data["result"]["data"][0]["address"]["city"])
      res.render("results", { data: data });
    }
  });
  // res.send("Hello, it works!");
});

var port = process.env.PORT || 3000;

app.listen(port, process.env.IP, function() {
  console.log("running on port: " + port);
});

now in the end the output gives me:

    1| <h1>Results Page!!</h1>
    2| 
 >> 3| <%data["result"].forEach(function(data){%>
    4| <li><%= data["address"]%></li>
    5| <% }) %>
    6| 

data.result.forEach is not a function...

can someone let me know where it's disconnected?

I'm thinking the key should be in the URL itself, though the key is already in the header. I'm not too sure.

答案1

得分: 1

Your code assumes data["result"] is an array, but it is not. How do I know this? .forEach() requires an array to work. If it's used on something other than an array, you get that message.

You called res.render('results', {data: data})

Without knowing for sure, I guess your data object is fairly complex. This commented-out line:

res.send(data["result"]["data"][0]["address"]["city"])

makes it look like there's an array at data.result.data. (The giveaway is the index [0].) If that's true your ejs code should say

<%data.result.data.forEach(function(item){%>
<li><%= item.address%></li>
<% }) %>

I changed function(data) to function(item), because otherwise the data parameter name in the function invocation shadows the name data from its caller, and that's just confusing.

You can find out your data object's structure by doing console.log(data) and looking at the output, or you can read the docs for the api.

英文:

Your code assumes data[&quot;result&quot;] is an array, but it is not. How do I know this? .forEach() requires an array to work. If it's used on something other than an array, you get that message.

You called res.render(&#39;results&#39;, {data: data})

Without knowing for sure, I guess your data object is fairly complex. This commented-out line:

  res.send(data[&quot;result&quot;][&quot;data&quot;][0][&quot;address&quot;][&quot;city&quot;])

makes it look like there's an array at data.result.data. (The giveaway is the index [0].) If that's true your ejs code should say

&lt;%data.result.data.forEach(function(item){%&gt;
&lt;li&gt;&lt;%= item.address%&gt;&lt;/li&gt;
&lt;% }) %&gt;

I changed function(data) to function(item), because otherwise the data parameter name in the function invocation shadows the name data from its caller, and that's just confusing.

You can find out your data object's structure by doing console.log(data) and looking at the output, or you can read the docs for the api.

huangapple
  • 本文由 发表于 2020年1月6日 23:35:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定