带有Node.js参数的Jade模板继承错误

时间:2015-01-26 04:00:19

标签: node.js express pug

我有一个我用作模板的玉器布局,我网站的每个页面都包含此模板。当我渲染布局时,我将配置对象传递给jade,该对象包含页面标题等信息。

当我尝试加载其中一个页面时,jade文件中存在错误

TypeError: /home/danielbraithwt/Programming/Node/Folio/views/layout.jade:4
2| html
3| head
> 4| title= config.name
5| link(rel='stylesheet', href='/stylesheets/layout.css')
6| link(rel='stylesheet', href='/stylesheets/bootstrap.min.css')
7| script(src='/javascripts/jquery-1.11.2.min.js')

Cannot read property 'name' of undefined
at eval (eval at <anonymous> (/home/danielbraithwt/Programming/Node/Folio/node_modules/jade/lib/index.js:190:8), <anonymous>:19:64)
at eval (eval at <anonymous> (/home/danielbraithwt/Programming/Node/Folio/node_modules/jade/lib/index.js:190:8), <anonymous>:282:22)
at res (/home/danielbraithwt/Programming/Node/Folio/node_modules/jade/lib/index.js:191:38)
at Object.exports.render (/home/danielbraithwt/Programming/Node/Folio/node_modules/jade/lib/index.js:316:10)
at Object.exports.renderFile (/home/danielbraithwt/Programming/Node/Folio/node_modules/jade/lib/index.js:352:18)
at View.exports.renderFile [as engine] (/home/danielbraithwt/Programming/Node/Folio/node_modules/jade/lib/index.js:337:21)
at View.render (/home/danielbraithwt/Programming/Node/Folio/node_modules/express/lib/view.js:93:8)
at EventEmitter.app.render (/home/danielbraithwt/Programming/Node/Folio/node_modules/express/lib/application.js:530:10)
at ServerResponse.res.render (/home/danielbraithwt/Programming/Node/Folio/node_modules/express/lib/response.js:904:7)
at module.exports (/home/danielbraithwt/Programming/Node/Folio/app.js:84:6)

作为模板的玉文件

doctype html
html
    head
        title= config.name
        link(rel='stylesheet', href='/stylesheets/layout.css')
        link(rel='stylesheet', href='/stylesheets/bootstrap.min.css')
        script(src='/javascripts/jquery-1.11.2.min.js')
        script(src='/javascripts/bootstrap.min.js')

        script
            | $(document).ready(function () {
            |   $('.dropdown-toggle').dropdown();
            | });

        block headtags
    body
        div(id="header", class="navbar navbar-default")
            div(class="container")
                div(class="navbar-header")
                    div(class="navbar-brand", href="/")= config.name

                div(class="collapse navbar-collapse")
                    ul(class="navbar-left nav navbar-nav")
                        li(class=(title === "Home") ? "active" : "")
                            a(href="/")= "Home"

                        li(class=(title === "Projects") ? "active" : "")
                            a(href="/projects")= "Projects"

                    ul(class="navbar-right nav navbar-nav")
                        if loggedIn
                            li
                                a(href="/logout")= "Logout"

                            li(class="dropdown")
                                a(id="edit_menu", href="#", class="dropdown-toggle", data-toggle="dropdown", aria-haspopup="true", roll="button", aria-expanded="false")= "Edit "
                                    span(class="caret")

                                ul(class="dropdown-menu", roll="menu", aria-labelledby="edit_menu")
                                    li(roll="presentation")
                                        a(href="/projects/new", roll="menuitem", tabindex="-1")= "Add Project"
                                        a(href="/update/config", roll="menuitem", tabindex="-1")= "Update Details"
                                        a(href="/update/login", foll="menuitem", tabindex="-1")= "Update Login"
                        else
                            li(class=(title === "Login") ? 'active' : '')
                                a(href="/login")= "Login"

        if (config.banner_image != '')
            div(id="image", style='background-image: url(uploads/' + config.banner_image + ');')
                div(id="header_image_text")
                    div(id="name")= (config.name != '' ? config.name : "Your Name")
                    div(id="desc")= (config.description != '' ? config.description : "Description About You")

        block content

扩展模板的布局

extends layout

block headtags
    link(rel='stylesheet', href='/stylesheets/editproject.css')

block content
    div(id="edit_project_form", class="col-sm-6 col-sm-offset-3")
        div(id="form_title")= "Edit Project"

        form(method="post", class="form_group", enctype="multipart/form-data")
            div(class="input-group input-group-lg col-sm-10 col-sm-offset-1")
                input(type="text", name="project_name", class="form-control", placeholder=project.name)

            div(class="input-group input-group-lg col-sm-10 col-sm-offset-1")
                input(type="textarea", name="project_description", class="form-control", placeholder=project.description)

            div(class="input-group input-group-lg col-sm-10 col-sm-offset-1")
                input(type="text", name="project_weblocation", class="form-control", placeholder=(project.weblocation != null ? project.weblocation : "Web Location"))

            div(class="input-group input-group-lg col-sm-10 col-sm-offset-1")
                input(type="text", name="project_sourcelocation", class="form-control", placeholder=(project.sourcelocation != null ? project.sourcelocation : "Source Code Location"))

            div(class="thumbnail_upload")
                div(class='sub_title')= "Thumbnail"
                p= "Size: 180px X 180px"
                div(class="project_thumbnail thumbnail")
                    img(src="../../images/" + project.thumbnail)
                input(type="file", value="project_thumbnail", name="project_thumbnail")

            div(class="feture")= "Feture project"
                input(type="checkbox", (project.checked ? "checked" : "") ,name="project_fetured")

            div(class="btn-group btn-group-lg")
                button(type="submit", class="btn btn-primary")= "Update"

最后我用来呈现页面的路线

router.get('/projects/edit/:id', function(req, res) {
        var loggedIn = isLoggedIn(req);

        // If the user isnt logged in then they cant do this action
        if (!loggedIn) {
            res.redirect('/login');
        }

        // Get the ID of the project to edit
        var id = req.params.id;
        req.getConnection(function(err, connection) {
            if (err) {
                console.log(err);
            }

            connection.query("SELECT * FROM projects WHERE id=" + id, function(err, rows) {
                if (err) {
                    console.log(err);
                    res.redirect('/');
                } else {
                    res.render('editproject', {project: rows[0], loggedIn: loggedIn, config: config});  
                }
            }); 
        });
    });

有关为何发生此错误的任何帮助将不胜感激。

0 个答案:

没有答案