检查变量的存在的

检查变量的存在的

本文介绍了在EJS模板(使用ExpressJS)中检查变量的存在的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在EJS github页面上,只有一个简单的例子:



示例

 <%if用户){%> 
< h2><%= user.name%>< / h2>
<%}%>

这似乎是检查名为user的变量的存在,如果存在,请做一些东东。 Duh,对吧?

我的问题是,如果用户变量不存在,为什么世界上Node会抛出一个ReferenceError?这使得上述例子无效。检查变量的存在的适当方法是什么?我是否希望使用一个try / catch机制,并抓住ReferenceError?

  ReferenceError:IncomingMessage.anonymous中没有定义
(在< anonymous>(/ usr / local /lib/node/.npm/ejs/0.3.1/package/lib/ejs.js:140:12))
在IncomingMessage。< anonymous> (/usr/local/lib/node/.npm/ejs/0.3.1/package/lib/ejs.js:142:15)
在Object.render(/ usr / local / lib / node /)。 npm / ejs / 0.3.1 / package / lib / ejs.js:177:13)
在ServerResponse.render(/usr/local/lib/node/.npm/express/1.0.7/package/lib /express/view.js:334:22)
在Object。< anonymous> (/Users/me/Dropbox/Projects/myproject/server.js:188:9)
在param(/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect $ / $ / 10)
在/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:152:27
在Object.restrict(/用户/我/ Dropbox / Projects / myproject / server.js:94:5)
在param(/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware /router.js:146:21)

我知道我可以通过简单的方式让这个错误消失在我的服务器代码中添加一个用户局部变量,但是这里的重点在于,我想在运行时使用/ else nullcheck类型模式来检查这些变量的存在。不存在的变量的例外对我来说似乎是可笑的。

解决方案

同样的方式, js, typeof foo =='undefined',或者由于locals是包含它们的对象的名称,可以执行 if(localals)富)。这只是原始的js:p


On the EJS github page, there is one and only one simple example:https://github.com/visionmedia/ejs

Example

<% if (user) { %>
    <h2><%= user.name %></h2>
<% } %>

This seems to be checking for the existence of a variable named user, and if it exists, do some stuff. Duh, right?

My question is, why in the world would Node throw a ReferenceError if the user variable doesn't exist? This renders the above example useless. What's the appropriate way to check for the existence of a variable? Am I expected to use a try/catch mechanism and grab that ReferenceError?

ReferenceError: user is not defined
    at IncomingMessage.anonymous (eval at <anonymous> (/usr/local/lib/node/.npm/ejs/0.3.1/package/lib/ejs.js:140:12))
    at IncomingMessage.<anonymous> (/usr/local/lib/node/.npm/ejs/0.3.1/package/lib/ejs.js:142:15)
    at Object.render (/usr/local/lib/node/.npm/ejs/0.3.1/package/lib/ejs.js:177:13)
    at ServerResponse.render (/usr/local/lib/node/.npm/express/1.0.7/package/lib/express/view.js:334:22)
    at Object.<anonymous> (/Users/me/Dropbox/Projects/myproject/server.js:188:9)
    at param (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:146:21)
    at pass (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:162:10)
    at /usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:152:27
    at Object.restrict (/Users/me/Dropbox/Projects/myproject/server.js:94:5)
    at param (/usr/local/lib/node/.npm/connect/0.5.10/package/lib/connect/middleware/router.js:146:21)

I understand that I could make this error go away by simply adding a "user" local variable in my server code, but the whole point here is that I want to check for the existence of such variables at runtime using your every day if/else nullcheck type pattern. An exception for a variable that doesn't exist seems ridiculous to me.

解决方案

The same way you would do it with anything in js, typeof foo == 'undefined', or since "locals" is the name of the object containing them, you can do if (locals.foo). It's just raw js :p

这篇关于在EJS模板(使用ExpressJS)中检查变量的存在的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:50