玉与哈巴狗

问题描述 投票:0回答:1

我有一个使用引擎玉的示例,但是玉现在是哈巴狗。如果我使用翡翠代码1:1:

 br
 hr
 if post.comments
  h3 Comments
  each comment, i in post.comments
   .comment
    p.comment-name #{comment.name}
    p.comment-body #{comment.body}
  br

发生以下错误:

  TypeError: C:\LearnNodejs\nodeblog\views\show.pug:20
  18|       each comment, i in post.comments
  19|        .comment
> 20|         p.comment-name #{comment.name}
  21|         p.comment-body #{comment.body}
  22|       br
  23|     h3 Add Comment
 Cannot read property 'name' of undefined

适合哈巴狗(尝试):

br
 hr
 if post.comments.name !== undefined
  h3 Comments
  each comment, i in post.comments
   .comment
    p.comment-name comment.name
    p.comment-body comment.body
br

显示以下内容:

Comments
comment.name
comment.body
comment.name
comment.body
comment.name
comment.body
comment.name
comment.body
comment.name
comment.body

javascript:

router.get('/show/:id', function(req, res, next) {  
Post.findById(req.params.id,function(err, post){
res.render('show',{
    'post': post
    });
  });
});

显示器的哈巴狗代码应该是什么样?感谢您的支持。

pug
1个回答
0
投票

Pug的interpolation syntax与Jade的大部分相同。您仍然需要将变量包装在#{}中才能将其输出为文本。

尝试一下:

if post.comments.name !== undefined
  h3 Comments
  each comment, i in post.comments
    .comment
      p.comment-name #{comment.name}
      p.comment-body #{comment.body}

如果不起作用,则post.comments对象的结构可能与您预期的不同。如果post.comments是一个数组,并且您要检查第一项是否具有名称,则该if语句可能需要更改为:

if post.comments[0].name !== undefined
© www.soinside.com 2019 - 2024. All rights reserved.