简体   繁体   中英

TypeError: req.flash is not a function -- (NodeJs) (connect-flash) -- I can not open the page

When I tried to log in from page, I got error

TypeError: req.flash is not a function

I explain the errors I got and the methods I tried

  1. If I delete this code console.log (req.flash ("validation_error")) code in the function named "registerFormunuGoster" in the auth_controller file, I can make a successful link to the page in the first step. If I do not delete this code, I cannot connect to the page successfully in the first step.

  2. The text I mentioned above is translated into code below.

  3. const registerFormunuGoster = (req, res) => { res.render("register", { layout: "./layout/auth_layout",}) }

  4. Let's say I write the code mentioned above and opened the page, after that I fill the form on my page and I get the same error whenever I press the submit button after filling out the form. To solve this problem, under the auth_controller.js file If I delete the code "req.flash (" validation_error ", errors)" in the function named "register" this time i get a different error.I am leaving the other error I get below. I think the reason I got such an error must be because I did res.redirect().

  5. Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

  6. The events I mentioned in item 4 are translated into code below.

  7. `const register = (req, res) => { const hatalar = validationResult(req);

     if (.hatalar.isEmpty()) { res.redirect("/register") } res,render("register": { layout. ",/layout/auth_layout" ,})

    }`

Below are my app.js and auth_controller.js configuration

app.js

const express = require("express")
const app = express()
const dotenv = require("dotenv").config()
const session = require("express-session")
const flash = require("connect-flash")
// database bağlantısı - veritabanı bağlantısı 
require("./src/config/database")

// TEMPALTE ENGİNE AYARALARI 
const ejs = require("ejs")
const expressLayouts = require("express-ejs-layouts")
const path = require("path")
app.set("view engine", "ejs")
app.set("views", path.resolve(__dirname, "./src/views"))
app.use(express.static("public"))
app.use(expressLayouts)


// routerlar include edilip kullanılır 
const authRouter = require("./src/routers/auth_router")
app.use("/", authRouter)


// formdan yollanılan verileri json a çevirip verilere dönüşmesi için bu şart
app.use(express.urlencoded({ extended: true }))


//* const session = require("express-session")
//* Seesion ve flash message
//* bunları yapmak için yukarıdaki modul lazım
//? önemli bir not çıldıurmak üzereydim kodlar çalışmıyordu kodların çalışması üçün üst sıralar çekince oldu bakalım neyden kaynaklanıyorumuş deneyerek bulucam 
//? app.get gibi sunucu istekleri yönlendirmeden önce kullanılması lazımmış
app.use(session({
    secret: process.env.SESSION_SECRET,
    resave: false,
    saveUninitialized: true,
    cookie: {
        maxAge:1000*5
    }
    //* maxAge: verilen cookienin ne kadar zaman sonra kendisini ihma etmesini söylüyor
    //* saniye cinsinden verdik 
}))
//? flash mesajlarının middleware olarak kullanılmasını sağladık yani aldığımız hatayı flash sayesinde kullanabilceğiz
app.use(flash())


let sayac = 0
app.get("/", (req, res) => {
    if (!req.session.sayac) {
        req.session.sayac = 1
    } else {
        req.session.sayac++
    }
    res.json({ message: "Hello World", sayac: req.session.sayac })
})



app.listen(process.env.PORT, _ => console.log(`Server started at ${process.env.PORT} port `))

auth_controller.js:

const { validationResult } = require("express-validator")

const loginFormunuGoster = (req, res) => {
    res.render("login", { layout: "./layout/auth_layout" })
}
const login = (req, res) => {
    res.render("login", { layout: "./layout/auth_layout" })
}



const registerFormunuGoster = (req, res) => {
    // console.log(req.flash("validation_error"))
    res.render("register", { layout: "./layout/auth_layout" ,})
}
const register = (req, res) => {
    const hatalar = validationResult(req);

// req.body adı altında girilen veriler gözüküyor
// console.log(req.body) 
// console.log(hatalar)
    if (!hatalar.isEmpty()) {
        // req.flash("validation_error",hatalar)
        res.redirect("/register")
    }
    res.render("register", { layout: "./layout/auth_layout" ,})
}


const forgetPasswordFormunuGoster = (req, res) => {
    res.render("forget_password", { layout: "./layout/auth_layout" })
}
const forgetPassword = (req, res) => {
    res.render("forget_password", { layout: "./layout/auth_layout" })
}


module.exports = {
    loginFormunuGoster,
    registerFormunuGoster,
    forgetPasswordFormunuGoster,
    register,
    login,
    forgetPassword,
}

I would really appreciate everyone's help!!! I hope I explained the error I received well

I think your problem come from a misunderstanding of how you should send you request back.

The second error you have 'Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client' indicates that the request you are trying to manipulate again is already finished (you can read a better explanation here: Error: Can't set headers after they are sent to the client )

So in your case, you cannot do a .redirect and a .render in the same request.

For your initial error, it seems that flash is not attached to the req object. I'm not sure but it might be because you are requiring it after your router and it is not ready when used in it. There:

// 'use' it before you require your auth_router
app.use(flash());

// then require your auth_controller.js file somewhere in this
const authRouter = require("./src/routers/auth_router")
app.use("/", authRouter)

Read connect-flash doc for more indication on how to use it: https://www.npmjs.com/package/connect-flash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM