Connect flash is not defiened

1. Connect flash is not defiened

Ricardo de Paula
ricardodepaula

(usa Ubuntu)

Enviado em 24/01/2020 - 15:28h

Oi! No módulo connec-flash, tenho um erro que não consigo identificar. Eu o revisei o código umas mil vezes. Não consigo prosseguir com meu projeto por causa disso. Modifiquei o escopo tentando identificar o erro, mas ele continua. Estou seguindo os passos de um livro e o problema parece não ser no código mas outro lugar.

Na página de registro:

ReferenceError: flash is not defined
at module.exports (/controllers/newUser.js:3:9)
at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
at next (/node_modules/express/lib/router/route.js:137:13)
at module.exports (/middleware/redirectAuhenticationMiddleware.js:6:5)
at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
at next (/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
at /node_modules/express/lib/router/index.js:281:22
at Function.process_params (/node_modules/express/lib/router/index.js:335:12)
at next (/node_modules/express/lib/router/index.js:275:10)
at /node_modules/connect-flash/lib/flash.js:21:5
at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/node_modules/express/lib/router/index.js:317:13)
at /node_modules/express/lib/router/index.js:284:7
at Function.process_params (/node_modules/express/lib/router/index.js:335:12)


index.js:

const express = require('express')

const path = require('path')
const ejs = require('ejs')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const fileUpload = require('express-fileupload')
const BlogPost = require('./models/BlogPost')
const expressSession = require('express-session')
const flash = require('connect-flash')


newUser.js:

module.exports = (req, res) => {
res.render('register', {errors:flash('validationErrors')})


}

storeUser.js:

 const User = require('../models/User')
const path = require('path')


module.exports = (req, res) => {
User.create(req.body, (error, user) => {
if (error){
const validationErrors = Object.keys(error.errors).map(key => error.errors[key].message)
req.flash('validationErrors', validationErrors)
//req.session.validationErrors = validationErrors
return res.redirect('/auth/register')
}
res.redirect('/')
})
}


Alguém pode ajudar?


  


2. Re: Connect flash is not defiened

Ricardo de Paula
ricardodepaula

(usa Ubuntu)

Enviado em 24/01/2020 - 18:10h

também configurei para ele ser usado globalmente:
index.js

const express = require('express')

const path = require('path')
const ejs = require('ejs')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const fileUpload = require('express-fileupload')
const BlogPost = require('./models/BlogPost')
const expressSession = require('express-session')
const flash = require('connect-flash')

//middlewares

const homeController = require('./controllers/home')

const newPostController = require('./controllers/newpost')

const aboutController = require('./controllers/about')

const contactController = require('./controllers/contact')

const blogController = require('./controllers/blog')

const getPostsController = require('./controllers/getpost')

const storePostsController = require('./controllers/store')

const newUserController = require('./controllers/newUser')

const storeUserController = require('./controllers/storeUser')

const loginController = require('./controllers/login')

const loginUserController = require('./controllers/loginUser')

const authMiddleware = require('./middleware/authMiddleware')

const redirectAuthenticationMiddleware = require('./middleware/redirectAuhenticationMiddleware')

const logoutController = require('./controllers/logout')

const app = new express()

mongoose.connect('mongodb://localhost/my_database', {useNewUrlParser:true})

// configurações
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:true}))
app.use(fileUpload())

app.use(expressSession({
secret:'keyboard cat',
saveUninitialized: true,
cookie: { secure: true },
resave: true,

}))

global.loggedIn = null
app.use("*", (req, res, next) =>{
loggedIn = req.session.userId
next()
})
app.use(flash());


// rotas

app.get('/', homeController)

app.get('/blog', blogController)

app.get('/post/:id', getPostsController)

app.get('/about/', aboutController)

app.get('/contact', contactController)

app.get('/posts/new/', authMiddleware, newPostController)

app.get ('/auth/register', redirectAuthenticationMiddleware, newUserController)

app.get('/auth/login', redirectAuthenticationMiddleware, loginController)

app.get('/auth/logout', logoutController)

app.post('/posts/store', authMiddleware, storePostsController)

app.post('/users/register', redirectAuthenticationMiddleware, storeUserController)

app.post('/users/login', redirectAuthenticationMiddleware, loginUserController)

app.use((req,res) => res.render('notfound'))

app.listen(4000,
()=>{
console.log(`servidor rodando na porta 4000`)
}
)







Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts