我想要顯示當前遊戲中的玩家以及他們使用此JSON的頻道,但它不斷給我這個錯誤:如果對象不存在於我正在使用的JSON中,我該如何返回錯誤或N/A
TypeError: /home/ubuntu/workspace/sportsapp/views/results.ejs:8
6| <% data["games"].forEach(function(game){ %>
7| <ul>
>> 8| <li><%= game["home"]["name"]%> Vs. <%=game["away"]["name"]%> <%=game["broadcast"]["network"][1]%>
9|
10|
11| </li>
Cannot read property 'network' of undefined
at eval (eval at <anonymous> (/home/ubuntu/workspace/sportsapp/node_modules/ejs/lib/ejs.js:524:12), <anonymous>:29:42)
at Array.forEach (native)
at eval (eval at <anonymous> (/home/ubuntu/workspace/sportsapp/node_modules/ejs/lib/ejs.js:524:12), <anonymous>:22:22)
at returnedFn (/home/ubuntu/workspace/sportsapp/node_modules/ejs/lib/ejs.js:555:17)
at tryHandleCache (/home/ubuntu/workspace/sportsapp/node_modules/ejs/lib/ejs.js:203:34)
at View.exports.renderFile [as engine] (/home/ubuntu/workspace/sportsapp/node_modules/ejs/lib/ejs.js:412:10)
at View.render (/home/ubuntu/workspace/sportsapp/node_modules/express/lib/view.js:126:8)
at tryRender (/home/ubuntu/workspace/sportsapp/node_modules/express/lib/application.js:639:10)
at EventEmitter.render (/home/ubuntu/workspace/sportsapp/node_modules/express/lib/application.js:591:3)
at ServerResponse.render (/home/ubuntu/workspace/sportsapp/node_modules/express/lib/response.js:960:7)
<h1>Who's Playing Today!</h1>
<h2>Mens NCAA CBB</h2>
<% data["games"].forEach(function(game){ %>
<ul>
<li><%= game["home"]["name"]%> Vs. <%=game["away"]["name"]%> <%=game["broadcast"]["network"][1]%>
</li>
</ul>
<% }); %>
<% include partials/footer %>
我假設這是因爲我想在我的EJS文件顯示對象不會在一些JSON部分存在像下面
{
id: "f172b5e1-97d5-4599-a033-fdafeb12de63",
title: "CBI - First Round - Game 6",
status: "scheduled",
coverage: "extended_boxscore",
scheduled: "2017-03-16T00:05:00+00:00",
conference_game: false,
venue: {
id: "56bfb904-5b8e-4241-bf79-4c02765df0ec",
name: "Swinney Recreation Center",
capacity: 2000,
address: "5030 Holmes St",
city: "Kansas City",
state: "MO",
zip: "64110",
country: "USA"
},
home: {
name: "UMKC Kangaroos",
alias: "UMKC",
id: "820c8f26-cf11-4997-b2e2-19f47bd2f72f"
},
away: {
name: "Green Bay Phoenix",
alias: "GB",
id: "61a3e5ab-1be3-4694-b83f-edae0953f409"
}
},
{
id: "23ee0924-a55f-4993-a31b-c344f209cd69",
title: "CBI - First Round - Game 5",
status: "scheduled",
coverage: "extended_boxscore",
scheduled: "2017-03-16T01:00:00+00:00",
conference_game: false,
venue: {
id: "32e70ac7-27f7-4a28-92a4-8584180cf3ca",
name: "Arena-Auditorium",
capacity: 15028,
address: "1000 E University Ave",
city: "Laramie",
state: "WY",
zip: "82071",
country: "USA"
},
home: {
name: "Wyoming Cowboys",
alias: "WYO",
id: "a7127c0a-a466-47ae-91ad-9a71ee2c9427"
},
away: {
name: "Eastern Washington Eagles",
alias: "EWU",
id: "54df21af-8f65-42fc-bc01-8bf750856d70"
}
},
其他時間確實有這個值:
{
id: "e288ca27-4559-4c41-95d2-de8716baca2f",
title: "Iowa Bracket - First Round - Game 1",
status: "scheduled",
coverage: "full",
scheduled: "2017-03-16T01:00:00+00:00",
conference_game: false,
venue: {
id: "04c2567a-3a02-462b-b06b-5a4216dd30af",
name: "Carver-Hawkeye Arena",
capacity: 15400,
address: "1 Elliot Drive",
city: "Iowa City",
state: "IA",
zip: "52242",
country: "USA"
},
broadcast: {
network: "ESPN2",
satellite: "209",
internet: "WatchESPN"
},
home: {
name: "Iowa Hawkeyes",
alias: "IOWA",
seed: 1,
id: "c10544de-e3bd-4776-ba2e-83df8c017fd1"
},
away: {
name: "South Dakota Coyotes",
alias: "SDAK",
seed: 8,
id: "c946b7ac-5159-4817-93c4-fc9495c6425b"
}
},
{
有誰知道如何做到這一點,如果它沒有這個值,它只會顯示N/A,如果它確實會顯示它所在的頻道?
'game.hasOwnProperty('network')'將返回false' – surajck
@surajck我很困惑如何寫這個或如何處理這些信息。 – Branduo