如果我正在使用的JSON中不存在对象,我该如何返回错误或N / A.

时间:2017-03-15 19:47:21

标签: javascript json node.js ejs

我想要显示今天玩游戏的人以及他们使用这个JSON的频道,但它一直给我这个错误:

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,如果它有,它会实际显示它所在的频道吗?

3 个答案:

答案 0 :(得分:0)

你可以通过以下几种方式做到这一点。

在JSON中,显式添加您期望的属性,并将N / A作为字符串属性放在JSON中。

{
...
network: ["N/A"] /* you can change the structure to whatever you need */
...
}

如果您无法控制JSON,您的javascript可以按摩数据并在您需要的位置添加字符串"N/A"

let json = fetchJson()
  .map(d => {
    if(!d.broadcast) {
      d.broadcast = {"network": ["N/A"]} // you can put whatever structure you need here
    }
  })

或者在你的ejs模板中

<li><%= game["home"]["name"]%> Vs. <%=game["away"]["name"]%> <%=(game["broadcast"] ? game["broadcast"]["network"][1] : "N/A")%>

在ejs模板中添加三元?可能是解决问题的方法,但在模板编译之前修饰数据是我在自己的项目中犯错误的地方。

答案 1 :(得分:0)

<li><%= game["home"]["name"]%> Vs. <%=game["away"]["name"]%> <%=game.hasOwnProperty('network') ? game["broadcast"]["network"][1] : "N/A"%>

这将检查它是否有一个broadcast键,然后如果它有,将抓住第二个网络(也许你想要第一个?如果是这种情况使用[0]),否则它将返回N/A

答案 2 :(得分:0)

我认为这应该足够简单:

(game.broadcast||{}).network ||"N/A"

像这样使用:

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

    </li>
</ul>

我注意到网络是一个字符串而不是一个数组,所以我假设你真的不想只显示它的第二个字母(例如"ESPN"[1] === "S"