检查 Jade 中是否存在某个字段
考虑一下 Jade 的这段代码:
if(#{episode[question.name][field]})
#{episode[question.name][field]}
我想检查episode[question.name][field]的值是否存在。如果是,则在下一行输出该值。
500 语法错误:意外的标记非法
这是当前源和更多信息。
h1= title
p 欢迎来到 #{title}
ol#questions
each question in questions
li
ul#question
li: h3 (#{question.name}) #{question.description}
- if(question.options)
ul#options
for option, i in question.options
if(typeof(option) === "object")
li: h3 #{i}
ul#option
each soption in option
li
input(type='checkbox', name='episode[#{question.name}][#{soption}]', checked='#{episode[question.name][soption]}')
//
label
#{soption}
else
li
input(type='checkbox', name='episode[#{question.name}][#{option}]', checked='#{episode[question.name][option]}')
//
label
#{option}
if(typeof(question.fields) !== 'undefined')
for field, i in question.fields
if(field === "Date")
p.date
if(#{episode[question.name][field]} !== "undefined")
#{episode[question.name][field]}
else
p
input(name='episode[#{question.name}][#{field}]', class='', value="#{episode[question.name][field]}")
,这里是传入的数据对象
episode = {
"form": {
"formid": "4efd9c4cae999dcf0a12c461",
"name": "ROC"
},
"patientid": ObjectId("4ef6835a7a5869082a80bc95"),
"certperiodid": ObjectId("4f020ff0850b21bc2b000001"),
"M0080": {
"2-PT": "on",
"3-SLP\/ST": "on",
"4-OT": "on"
},
"M0090": {
"Date": "2012-01-02"
},
"M0100": {
"1 - Start of care--further visits planned": "on",
"3 - Resumption of care (after inpatient stay)": "on"
},
"M00104": {
"Date": "2012-05-02"
},
"_id": ObjectId("4f071a0424ddf0dd66000003")
}
如何修复并正确写入?
因此,例如,如果其中一个日期字段为空(未填写),为了防止错误,我需要在输出值之前检查它是否不为空。
再次提前致谢
Consider this snippet of Jade:
if(#{episode[question.name][field]})
#{episode[question.name][field]}
I want to check if the value of episode[question.name][field]
exists. If it does, output the value on the next line.
500 SyntaxError: Unexpected token ILLEGAL
Here is the current source and more information.
h1= title
p Welcome to #{title}
ol#questions
each question in questions
li
ul#question
li: h3 (#{question.name}) #{question.description}
- if(question.options)
ul#options
for option, i in question.options
if(typeof(option) === "object")
li: h3 #{i}
ul#option
each soption in option
li
input(type='checkbox', name='episode[#{question.name}][#{soption}]', checked='#{episode[question.name][soption]}')
//
label
#{soption}
else
li
input(type='checkbox', name='episode[#{question.name}][#{option}]', checked='#{episode[question.name][option]}')
//
label
#{option}
if(typeof(question.fields) !== 'undefined')
for field, i in question.fields
if(field === "Date")
p.date
if(#{episode[question.name][field]} !== "undefined")
#{episode[question.name][field]}
else
p
input(name='episode[#{question.name}][#{field}]', class='', value="#{episode[question.name][field]}")
and here is the data object passed in
episode = {
"form": {
"formid": "4efd9c4cae999dcf0a12c461",
"name": "ROC"
},
"patientid": ObjectId("4ef6835a7a5869082a80bc95"),
"certperiodid": ObjectId("4f020ff0850b21bc2b000001"),
"M0080": {
"2-PT": "on",
"3-SLP\/ST": "on",
"4-OT": "on"
},
"M0090": {
"Date": "2012-01-02"
},
"M0100": {
"1 - Start of care--further visits planned": "on",
"3 - Resumption of care (after inpatient stay)": "on"
},
"M00104": {
"Date": "2012-05-02"
},
"_id": ObjectId("4f071a0424ddf0dd66000003")
}
How can this be fixed and written correctly?
so for example if one of the date fields are empty (not filled in), in order to prevent an error , i need to check if its not empty, before outputting the value.
thanks again in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
表达式不应使用转义:(
显然,假设变量设置正确。)
编辑以响应评论
在尝试读取字段之前,您不会先检查该元素是否存在,在日期显示和输入元素...虽然您的用例仍然有点不透明,但您需要首先检查元素:
我认为您正在尝试为没有值的事物显示输入元素,并以其他方式显示它——不太确定。你可能想要更接近这个的东西(不知道你在做什么,我做了一个假模板,我认为它反映了你的数据——我评论了
“2-PT”
值进行测试)。一些小的重构来清理东西,但不多。整理后:
将
"2-PT"
行注释掉:Expressions shouldn't use escaping:
(Assuming proper variable setup, obviously.)
Edit to respond to comment
You're not checking to see if the element exists first before trying to read the field, in both the date display and the input element... While your usecase is a bit opaque still, you need to check for the element first:
I think you're trying to show an input element for things without values, and display it otherwise--not quite sure. You may want something closer to this (w/o knowing what you're doing, I made a fake template w/ what I think mirrors your data--I commented the
"2-PT"
value out to test). Some minor refactoring to clean things up, but not much.After tidying:
With the
"2-PT"
line commented out: