Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 获取错误-ejs文件中未定义消息(在express中使用flash时)_Node.js_Express_Ejs_Express Session_Passport Local - Fatal编程技术网

Node.js 获取错误-ejs文件中未定义消息(在express中使用flash时)

Node.js 获取错误-ejs文件中未定义消息(在express中使用flash时),node.js,express,ejs,express-session,passport-local,Node.js,Express,Ejs,Express Session,Passport Local,我正在尝试打印ejs文件中的flash消息。但这是一个错误 **ReferenceError: E:\nodeProject\views\login.ejs:10 8| <body> 9| <h1>Login</h1> >> 10| <% if(messages.error){%> 11| <%=messages.error%> 12| <

我正在尝试打印ejs文件中的flash消息。但这是一个错误

**ReferenceError: E:\nodeProject\views\login.ejs:10
    8| <body>
    9|     <h1>Login</h1>
 >> 10|     <% if(messages.error){%>
    11|         <%=messages.error%>
    12|         <%}%>
messages is not defined.**
这是我的passport-config.js文件。在这里,我声明了passport在出现错误时应显示为错误的消息。我的passport-config.js文件是:

const initializePassport=require('./passport-config');
const passport=require('passport');
const flash=require('express-flash');
const session=require('express-session');
const router=express.Router();

app.use(session({
    secret:process.env.SESSION_SECRET,
    resave :false,//if we want to resave the session variables if nothing has changed
    saveUninitialized:false//do u want to save an empty value if there is no value

}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session()); 

router.route('/login').post(passport.authenticate('local',{
    successRedirect:'/',
    failureRedirect:'/login',
    failureFlash:true, 
}));

initializePassport(passport,email=>{
    MongoClient.connect(url,(err,db)=>{
        if(err) throw err;
        const dBase=db.db();
        dBase.collection('Users').find({email:email},(err,res)=>{
            if(err) throw err;
            console.log(res);
        })
        return res;
        db.close();
    });    
});
const localStrategy=require('passport-local').Strategy
const bcrypt=require('bcrypt');

function initialize(passport,getUserByEmail){
    const authenticateUser= async(email,password,done)=>{
        const user=getUserByEmail(email);
        if(user==null)
        {
            return done(null,false,{message:"No user with this email found"});                                                                    
        }
        try {
            if(await bcrypt.compare(password,user.password)){
                return done(null,user);
            }
            else{
                return done(null,false,{message:"Password does not match "});
            }

        } catch (error) {
            return done(error);          
        }
    }
    passport.use(new localStrategy({userName:'email'},authenticateUser));  
}
module.exports= initialize;
**这是ejs文件,我在其中使用messages.error并获取未定义消息的错误:**

<body>
    <h1>Login</h1>
    <% if(messages.error){%>
        <%=messages.error%>
        <%}%>



    <form action="/register" method="post">

    <div>
        <label for="name">Name</label>
        <input type="text" id="name" name="name" required>
    </div>
    <div>
        <label for="email">Email</label>
        <input type="email" id="email" name="email" required>
    </div>
    <div>
        <label for="password">Password</label>
        <input type="password" id="password" name="password" required>
    </div>
    <button type="submit">Login</button>
    </form>

    <a href="/register">Register</a>
</body>

登录
名称
电子邮件
密码
登录
请看一下,告诉我出了什么问题。我是一个初学者,所以如果我的提问方式不合适,请不要介意。谢谢