1

So I'm trying to get my .ejs file to display some more information from my API code but it keeps giving me an error code.

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

I'm trying to get my ejs to display the game's home team name, away team name and also what network that the game is on, but I can only figure out how to display the team names for both the home and away teams.

Below is the API I am using

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

Here is my ejs file

<% data["games"].forEach(function(game){ %>
    <ul>
        <li><%= game["home"]["name"]%> Vs.<%=game["away"]["name"]%>
        <ul>
            <li><%= game["broadcast"]["network"]%></li>
        </ul>
        </li>
    </ul>
<% }); %>

And this is my express node app with the API key removed.

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");
});

I have a feeling I'm doing something wrong with the looping part of the ejs file but I'm not sure exactly where I'm screwing up. Any help would be greatly appreciated.

Branduo
  • 186
  • 2
  • 12
  • log your game["broadcast"] value, are you sure all games in this array has property of game.broadcast.network? – Artur Feb 18 '17 at 05:29
  • @Kotel_ when I console.logged it it gave me this TypeError: Cannot read property 'broadcast' of undefined So I'm assuming not every array has that value, how do I go about getting it to display the ones that do have that value and then just display a blank if it doesn't? – Branduo Feb 18 '17 at 05:36
  • I think @Kotel_ is right here.... while the JSON entry you've shown *does* have a `broadcast` key, it's most likely that there is an entry that *doesn't* and is causing the error you're seeing. You should check for its existence first before trying to access it. – mscdex Feb 18 '17 at 06:02
  • @mscdex Yes he was correct, so now I'm wondering how I access the ones that do exist without getting that error. Or how do I replace the ones that don't exist with "does't exist" or something similar? Should I do a loop? – Branduo Feb 18 '17 at 06:08
  • @Branduo you can use if statement and print only if property exist: https://stackoverflow.com/questions/8216918/can-i-use-conditional-statements-with-ejs-templates-in-jmvc `
  • <% (game.home ? game.home.name : 'does\'t exist') %> Vs.<% (game.away ? game.away.name : 'does\'t exist')%>`
  • – Artur Feb 18 '17 at 06:21
  • @Kotel_ thank you for the help! – Branduo Feb 18 '17 at 06:29