在Node.JS中的EJS中检查对象中的未定义属性

时间:2018-05-19 21:14:23

标签: javascript node.js ejs

我有一个名为user的对象,可能有也可能没有定义子属性。例如,有时候没有“pages”对象,有时你可以去user.pages.someother变量。

我可以在EJS中看到如何检查该用户是否存在,但是如何检查user.pages.someothervariable是否存在而没有“无法访问未定义的属性”错误。

我已经尝试了这个和typeof,但无法让它工作。

<!-- Home page of slothy.cloud -->
<!DOCTYPE html>
<html>
<!--
	My small cozy website.
		© veryslothysloth 2018
-->
	<head>
		<!-- Links -->
		<link rel="apple-touch-icon" sizes="180x180" href="img/favicon/apple-touch-icon.png">
		<link rel="icon" type="image/png" sizes="32x32" href="img/favicon/favicon-32x32.png">
		<link rel="icon" type="image/png" sizes="16x16" href="img/favicon/favicon-16x16.png">
		<link rel="manifest" href="site.webmanifest">
		<link rel="mask-icon" href="img/favicon/safari-pinned-tab.svg" color="#2e86c1">
		<link rel="stylesheet" type="text/css" href="style.css">
		<link rel="stylesheet" href="css/font-awesome.min.css">

		<!-- Open Graph Protocol -->
		<meta property="og:url" content="://slothy.cloud" />
		<meta property="og:type" content="website" />
		<meta property="og:title" content="slothy.cloud | by veryslothysloth" />
		<meta property="og:description" content="A small slothy website." />
		<meta property="og:image" content="img/slothyicon.png" />
		<meta property="og:image:secure_url" content="img/slothyicon.png" />

		<!-- Twitter card -->
		<meta name="twitter:card" content="summary">
		<meta name="twitter:title" content="slothy.cloud |by veryslothysloth">
		<meta name="twitter:description" content="A small slothy website.">
		<meta name="twitter:image" content="img/slothyicon.png">
		<meta name="twitter:image:src" content="img/slothyicon.png">

		<!-- Data -->
		<title>slothy.cloud | by veryslothysloth</title>
		<meta charset="utf-8">
		<meta name="description" content="A small slothy website.">
		<meta name="keywords" content="sloth,slothy,veryslothysloth,file,upload,hosting,lolisafe">
		<meta name="viewport" content="width=device-width,initial-scale=1.0">
		<meta name="msapplication-TileColor" content="#2d89ef">
		<meta name="theme-color" content="#aed6f1">
	</head>

	<body>
		<header>
			<!-- Header -->
			<div id="header-inner">
				<a href="index.html" id="logo"></a>
				<nav>
					<a href="#" id="menu-icon"></a>
					<ul>
						<li><a href="index.html" class="current">Home</a></li>
						<li><a href="#">Cloud</a></li>
						<li><a href="#">Stat</a></li>
						<li><a href="#">Contact</a></li>
					</ul>
				</nav>
			</div>
		</header>


		<!-- Main Body -->
		<div class="container">
			<video loop>
				<source src="img/alley.mp4" type="video/mp4">
			</video>
			<!--<div class="container-inner">
				<p>Test Text</p>
			</div>-->
		</div>


		<!-- Footer -->
		<footer>
			<ul class="social">
				<li><a href="https://twitter.com/veryslothysloth" target="_blank"><i class="fa fa-twitter"></i></a></li>
				<li><a href="https://www.instagram.com/veryslothysloth/" target="_blank"><i class="fa fa-instagram"></i></a></li>
				<li><a href="https://steamcommunity.com/id/veryslothysloth" target="_blank"><i class="fa fa-steam"></i></a></li>
				<li><a href="mailto:contact@slothy.cloud" target="_blank"><i class="fa fa-at"></i></a></li>
			</ul>
		</footer>

		<!-- Second Footer -->
		<footer class="second">
			<p>&copy; veryslothysloth</p>
		</footer>
	</body>
</html>

我收到此错误:

<% if(locals.user.pages.pageVisits){ %>foo defined<% }else{ %>foo undefined<% } %>

2 个答案:

答案 0 :(得分:1)

您可以使用短路&& -

if(locals.user.pages && locals.user.pages.pageVisits) { /* do sth */ }

如果user.pages是假的,评估将无法进行。

如果链变得太长,您可以尝试将其封装到函数中,例如 -

function getPage(user) {
    return (user && user.pages && user.pages.accountPage)
        || "0"; // a fallback if the left side is falsy
}

答案 1 :(得分:0)

您可以通过运行if(local.user.pages)来检查您的用户是否有网页字段。这是因为在JS中的if语句中几乎可以评估任何值。如果user.pagesnull,则if语句将返回false。如果user.pages存在,则返回true。

你可以尝试一下以避免混乱:

var obj = {
  test: "hello"
}

try {
  console.log(obj.fakeKey.otherFakeKey)
}
catch(ex) {
  console.log("Caught")
}