node.js - 学着网上做express登陆,报错message is not defined
怪我咯
怪我咯 2017-04-17 14:55:43
0
6
1051

似乎是ejs部分有问题,在login.html中有一句 <%- message %> 报错

/Users/zhangwen/demo/views/header.html:6 4| <meta charset="utf-8"> 5| <title><%= title %></title> >> 6| <link href="/stylesheets/bootstrap.min.css" rel="stylesheet" media="screen"> 7| </head> 8| <body screen_capture_injected="true"> message is not defined

ReferenceError: /Users/zhangwen/demo/views/header.html:6
    4|     <meta charset="utf-8">
    5|     <title><%= title %></title>
 >> 6|     <link href="/stylesheets/bootstrap.min.css" rel="stylesheet" media="screen">
    7| </head>
    8| <body screen_capture_injected="true">

message is not defined
    at eval (eval at <anonymous> (/Users/zhangwen/demo/node_modules/ejs/lib/ejs.js:485:12), <anonymous>:22:16)
    at returnedFn (/Users/zhangwen/demo/node_modules/ejs/lib/ejs.js:514:17)
    at View.exports.renderFile [as engine] (/Users/zhangwen/demo/node_modules/ejs/lib/ejs.js:358:31)
    at View.render (/Users/zhangwen/demo/node_modules/express/lib/view.js:126:8)
    at tryRender (/Users/zhangwen/demo/node_modules/express/lib/application.js:639:10)
    at EventEmitter.render (/Users/zhangwen/demo/node_modules/express/lib/application.js:591:3)
    at ServerResponse.render (/Users/zhangwen/demo/node_modules/express/lib/response.js:961:7)
    at /Users/zhangwen/demo/routes/index.js:14:9
    at Layer.handle [as handle_request] (/Users/zhangwen/demo/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/zhangwen/demo/node_modules/express/lib/router/route.js:131:13)
怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(6)
小葫芦

Message needs to be defined when you render

阿神

Message is not defined, check the render or whether there is a message before

Peter_Zhu

You will know by looking at your error report,,, there is an error in line 14 of the index.js
file, here your rendering interface does not pass the message value required for page rendering,,,
Add an object parameter in render , pass in the value of your message

That’s it (˙-˙ )

洪涛

Was it written according to N-Blog? The ejs template will receive the properties defined in res.render. Post a simple example:

router.js:

 app.get('/article/:_id', function (req, res) {
    Post.getOneArticle(req.params._id, function(err, post){
        if(err){
            req.flash('error', err);
            return res.redirect('/');
        }
        res.render('article', {//这下面定义的就是你在ejs中接收的,例如message
            title: post.title,
            articleTitle: req.params.title,
            post: post,
            user: req.session.user,
            success: req.flash('success').toString(),
            error: req.flash('error').toString()
        });
    });
});

article.ejs

<article class="k-article">
      <h1><%= post.title %></h1>
      <%- post.post %>
      <hr/>
</article>
小葫芦

Thank you for your answers! At first, I followed the following two tutorials to implement the express login function, but the result was a message is not defined error. I was speechless. But last night, I redid the two tutorials separately, and they both ran successfully. I am really disappointed. I know what went wrong the first time. I followed the tutorial step by step. It seems that the tutorial is ok. Thank you again for your answers! ??

These two tutorials are similar and there are no problems
http://www.cnblogs.com/Leo_wl...

http://www.cnblogs.com/Leo_wl...

小葫芦

app.use(function(req, res, next){
This is too heavy to write. I worked on it for a week before I found out. Let’s see if anyone is the same as me.
By default, it is registered on // error handlers. Yes, I read the tutorial and rewrote it again when writing "session configuration" above
app.use(express.static(path.join(__dirname, 'public'))). As a result, the following was missing. Take effect, only the above takes effect

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template