javascript - Node.js 中 Jade遍历对象的问题。
天蓬老师
天蓬老师 2017-04-10 16:34:34
[JavaScript讨论组]

报错信息:

读取config.yml:

function read(file){
    try {
        var doc = yaml.safeLoad(fs.readFileSync(file, 'utf8'));
        console.log(doc);
        return doc;
    } catch (e) {
        console.log(e);
    }

}

config.yml

db: "mongodb://localhost/blog"
title: "My Blog"
nav:
  - 首页: ""
  - 目录: ""
  - Github: ""
  - 关于我: ""

route:

   app.get('/', function(req, res, next) {
        service.findAll(function(err, data){
            if(err){
                res.render('db error', err);
            }
            var _nav = config.read('./config.yml');
            var Article = {};
            Article.articles = data;//mongodb 的数据
            Article.nav = _nav.nav;
            res.render('index', Article);
        });

    });

index.jade:

extends layout


block main
    section.container
        ul.home.post-list
            each item in articles
            li.post-list-item
                article.post-block
                    h2.post-title
                        a.post-title-link(href="post.html") = item.title
                    .post-meta
                        .post-time = item.time
                    .post-content
                        p = item.content
                    a.read-more(href="2015/inline-and-absolute/") - 查看更多 -

append scripts
    script(src='/plugin/bower_components/jquery/dist/jquery.min.js')
    script(src='/javascripts/c.js')

layout.jade:

doctype html
html
  head
    title= title
      meta(charset="utf-8")
      meta(http-equiv="X-UA-Compatible", content="IE=edge")
      meta(name="description", content="")
      meta(name="viewport", content="width=device-width, initial-scale=1")
      link(rel='stylesheet', href='/stylesheets/style.css')
    block head
  body
      header
          a.logo-link(href="/")
              img(src="http://xc66.cc/i/i/xc.jpg")
          ul.nav.nav-list
              each item in nav
                  each val, key in item
                    li.nav-list-item
                    a.nav-list-link.active(href="#{val}", target="_self") #{key}
      block main
    block scripts
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(2)
怪我咯

下一行,缩进!

迷茫

@hsfzxjy

    15|           ul.nav.nav-list
  > 16|               each item in nav
    17|                   each val, key in item
    18|                     li.nav-list-item
    19|                     a.nav-list-link.active(href="#{val}", target="_self") #{key}

Cannot read property 'length' of undefined
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号