Wednesday, 15 March 2017

How can I return an error or an N/A if an object doesnt exist in the JSON I'm using

I'm wanting to display who is playing in today's games as well as what channel they are on using this JSON, but it keeps giving me this error:

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 %>

I'm assuming that it is because the object that I am trying to display in my ejs file doesn't exist in some of the JSON sections like below

{
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"
}
},

Where other times it does have that value:

{
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"
}
},
{

Does anyone know how to make it so if it doesn't have that value it will just display N/A and if it does it will actually display the channel it is on?



via Branduo

No comments:

Post a Comment