Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

a flash message is not showing up

I'm trying to create a register/login system and I want when the user tries to register with an existing email to have a flash message saying that email already exist but that message does not show app here's the code :

registerController.js:

const {validationResult} = require('express-validator');
const userController            = require('./user.controller');
let getRegisterPage = (req ,res) =>{
    return res.render('register.ejs' , {errors : req.flash("errors")});
    
};

let createNewUser = async (req , res) =>{
        let errorArr = [];
        let validationErrors = validationResult(req);
        if(!validationErrors.isEmpty()){
            let errors = Object.values(validationErrors.mapped());
            errors.forEach((item) =>{
                errorArr.push(item.msg);
            });
            req.flash("errors" , errorArr);
            return res.redirect("/register");
        }   

        try{
            let newUser = {
                username: req.body.username,
                email: req.body.email,
                password: req.body.password,
                birthday: req.body.birthday
            };
            return await userController.createNewUser(newUser);
            // return res.redirect("/login");
        }catch(e){
            req.flash( "error" , e );
        console.log("error cathced!");
            return res.redirect("/register");
            
        }
};

module.exports = {
    getRegisterPage : getRegisterPage,
    createNewUser : createNewUser

};

user.controller.js :

const connection = require('../config/db.config');




    let createNewUser = function(user){
        return new Promise(async(resolve , reject)=>{
            let emailCheck = await checkExistEmail(user.email);
            if(emailCheck){
                reject("email already exists. please use another email");
                console.log("email already exists. please use another email");
            }else{
                let userInfo = {
                    username: user.username,
                    email: user.email,
                    password : user.password,
                    birthday: user.birthday
                };
                connection.query("INSERT INTO users SET = ?" , userInfo , function(error , result){
                    if(error){
                        reject(error);
                    }else{
                        resolve("user created successfuly");
                    }
                });
            }
            
        });
    }



let checkExistEmail = (email) => {
    return new Promise( (resolve, reject) => {
        try {
            connection.query('SELECT * FROM users WHERE email = ?', email,
                function(err, rows) {
                    if (err) {
                        reject(err)
                    }
                    if (rows.length > 0) {
                        resolve(true)
                    } else {
                        resolve(false)
                    }
                }
            );
        } catch (err) {
            reject(err);
        }
    });
};
module.exports = {
    createNewUser: createNewUser
};

all other error flash messages appear except for this one that I mentioned also I get in the console "email already exists. please use another email" and "error cathced!" but that flash message is not showing up !!!

question from:https://stackoverflow.com/questions/65841367/a-flash-message-is-not-showing-up

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...