2017-03-15 18 views
-1

我想要顯示當前遊戲中的玩家以及他們使用此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,如果它確實會顯示它所在的頻道?

+0

'game.hasOwnProperty('network')'將返回false' – surajck

+0

@surajck我很困惑如何寫這個或如何處理這些信息。 – Branduo

回答

0

您可以通過多種方式來做到這一點。

在JSON中,顯式添加您期待的屬性並將JSON中的N/A作爲字符串屬性。

{ 
... 
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模板三元?可能去在溶液滴的方式,但在此之前的模板編譯疏導的數據是我往往在我自己的項目中犯錯。

+0

感謝您的幫助!你能解釋一下你的意思是三元的嗎?你的例子有效,但我想知道你在這裏做了什麼。 – Branduo

+0

我只是指[三運營商](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)它基本上是一個*內聯if語句* –

+0

好的,謝謝,會閱讀! – Branduo

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

這將檢查,看它是否有一個broadcast鍵,然後如果這樣做,將抓住第二網絡(也許你想第一?如果是這樣的話使用[0]),否則將返回N/A

+0

這工作,謝謝你的幫助! – Branduo

+0

這是錯誤的。 'game.hasOwnProperty(「network」)'如果存在'game.network',''返回'true' **如果'game.broadcast.network'存在,則返回** **。 –

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"

+0

你是對的,我不想要那裏1!謝謝你的幫助! – Branduo

相關問題