【问题标题】:Node JS Pug template loop errorsNode JS Pug 模板循环错误
【发布时间】:2020-02-24 05:09:21
【问题描述】:

在 pug 模板中,我尝试使用以下代码块打印错误数组(如果已定义)中的所有错误:

if errors !== "undefined"
    each item in errors
        .msgError Error: 
            = item.msg
else
    div success!

如果定义了数组“errors”,它将按预期工作。但是,我不知道为什么即使没有定义数组“errors”,它仍然会进入真正的分支并打印数组的项目,然后我得到了这个:

messages.pug:2 1| if errors !== "undefined" > 2| each item in errors 3| .msgError Error: 4| = item.msg 5| else Cannot read property 'length' of undefined

如果我把它写成这样的多行 javascript 块:

-
    if errors !== "undefined"
        each item in errors
            .msgError Error: 
                = item.msg
    else
        console.log(errors)

我收到了这个错误:

Unexpected token (106:3)
SyntaxError: Unexpected token (106:3)

请指教。谢谢。

【问题讨论】:

    标签: javascript node.js pug ejs


    【解决方案1】:

    去掉 undefined 周围的引号。

    “未定义”和未定义的含义不同。一个是字符串,另一个是未定义的值。

    if errors !== undefined
        each item in errors
            .msgError Error: 
                = item.msg
    else
        div success!
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-19
      • 2012-02-02
      • 1970-01-01
      • 2016-09-07
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多