所以我试图让我的.ejs文件显示我的API代码中的更多信息,但它一直给我一个错误代码。
TypeError: /home/ubuntu/workspace/sportsapp/views/results.ejs:7
5| <li><%= game["home"]["name"]%> Vs.<%=game["away"]["name"]%>
6| <ul>
>> 7| <li><%= game["broadcast"]["network"]%></li>
8| </ul>
9| </li>
10| </ul>
Cannot read property 'network' of undefined
我正试图让我的ejs显示游戏的主队名称,客队名称以及游戏所在的网络,但我只能弄清楚如何显示主场和客场的球队名称团队。
以下是我正在使用的API
{
id: "6641572c-b588-4b0e-8671-e279ced1985b",
status: "scheduled",
coverage: "full",
scheduled: "2017-02-18T17:00:00+00:00",
conference_game: true,
venue: {
id: "e1276102-db24-4188-9558-f249d69526f5",
name: "William D. Mullins Center",
capacity: 9493,
address: "200 Commonwealth Ave",
city: "Amherst",
state: "MA",
zip: "1003",
country: "USA"
},
broadcast: {
network: "NBCSN",
satellite: "220"
},
home: {
name: "Massachusetts Minutemen",
alias: "MASS",
id: "88104678-e53b-43b3-82f7-efb3a11cedb9"
},
away: {
name: "Davidson Wildcats",
alias: "DAV",
id: "2920c5fa-1e86-4958-a7c4-1e97b8e201d8"
}
},
这是我的ejs文件
<% data["games"].forEach(function(game){ %>
<ul>
<li><%= game["home"]["name"]%> Vs.<%=game["away"]["name"]%>
<ul>
<li><%= game["broadcast"]["network"]%></li>
</ul>
</li>
</ul>
<% }); %>
这是我的快速节点应用程序,删除了API密钥。
var express = require("express");
var app = express();
var request = require ("request");
app.set("view engine", "ejs");
// this is getting information from the API and returning results in the ejs.
app.get("/sportsapp", function(req, res){
request("https://api.sportradar.us/ncaamb-t3/games/2017/02/18/schedule.json?api_key=", function(error, response, body){
if(!error && response.statusCode == 200){
var data = JSON.parse(body);
res.render("results", {data : data});
}
});
});
app.listen(process.env.PORT, process.env.IP, function(){
console.log("Sports App has Started");
});
我有一种感觉,我对ejs文件的循环部分做错了但是我不确定我到底搞砸了。任何帮助将不胜感激。