简体   繁体   中英

TypeError: req.flash is not a function (NodeJs)

Working with Registration in a Site. For the register Form,Validation is done using mongoose models and trying to Use Flash to display the error message in the Form.

But struct at this error

TypeError: res.flash is not a function at /home/work/first-node-app/firstnode/routes/index.js:35:11

My Code snippets Follows below

App.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var mongoose = require('mongoose');
var passport = require('passport');
var session = require('express-session');
var flash        = require('connect-flash');

var indexRouter = require('./routes/index');
var commonKeys = require('./config/config');


var app = express();

//connect mongoDb 
mongoose.connect('mongodb://localhost:27017/db', { useNewUrlParser: true });

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// required for passport session
app.use(session({
  secret: 'secrettexthere',
  saveUninitialized: true,
  resave: true,
  // using store session on MongoDB using express-session + connect  
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());


app.use('/', indexRouter);

module.exports = app;

Index.js

var express = require('express');
var router = express.Router();
var passport = require('passport');
var User = require('../models/user-model');
var Local = require('../config/passport-local');

router.get('/register', function(req, res){
  res.render('public/register',{title:'Register'});
});


router.post('/register', function(req,res,next){
  User.create({name:req.body.name, password:req.body.password, email : req.body.email}, function(err, user){
    if (err) {      
      console.log('Error Inserting New Data');

      if (err.name == 'ValidationError') {
          for (field in err.errors) {
            res.flash(field, err.errors[field].message); 
          }
          res.render('public/register');
      }
    }
    if(user) {
      res.send('user registered');
    }    
  })    
});

module.exports = router;

Can anyone point out the error in this code snippets, as am new to Nodejs and Trying to work out the Form validation. The validation Works fine and the console displays the error but i can't pass that using flash. Can anyone Help me? And its much appreciated if you confirm my approach to code Nodejs is in correct way .

Use req.flash() in place of res.flash. Refer here for more clarification.

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