节点 JS Pug 模板循环错误

Node JS Pug template loop errors

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

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)

请指教。谢谢。

删除 undefined 周围的引号。

"undefined" 和 undefined 意思不同。一个是字符串,一个是未定义的值。

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