Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.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
Javascript &引用;TypeError:req.flash不是一个函数;使用带有nodejs、用户名和密码身份验证的passport_Javascript_Node.js_Passport.js - Fatal编程技术网

Javascript &引用;TypeError:req.flash不是一个函数;使用带有nodejs、用户名和密码身份验证的passport

Javascript &引用;TypeError:req.flash不是一个函数;使用带有nodejs、用户名和密码身份验证的passport,javascript,node.js,passport.js,Javascript,Node.js,Passport.js,这是运行和输入错误密码时的控制台输出 info: Listening on 127.0.0.1:3000 debug: GET / debug: Incorrect password /home/bob/git/authenticate-nodejs-prototype/node_modules/mongodb/lib/utils.js:98 process.nextTick(function() { throw err; });

这是运行和输入错误密码时的控制台输出

info: Listening on 127.0.0.1:3000
debug: GET /
debug: Incorrect password
/home/bob/git/authenticate-nodejs-prototype/node_modules/mongodb/lib/utils.js:98
    process.nextTick(function() { throw err; });
                                  ^

TypeError: req.flash is not a function
    at allFailed (/home/bob/git/authenticate-nodejs-prototype/node_modules/passport/lib/middleware/authenticate.js:118:15)
这是实际代码。你知道这是什么原因吗

var express = require('express'),
    app = express(),
    http = require('http').Server(app),
    winston = require('winston'),
    passport = require('passport'),
    LocalStrategy = require('passport-local').Strategy,
    ipaddress = '127.0.0.1',
    port = 3000,
    MongoClient = require('mongodb').MongoClient,
    ObjectId = require('mongodb').ObjectID,
    assert = require('assert'),
    mongoUrl = 'mongodb://' + ipaddress + ':27017/authenticate-nodejs-prototype',
    flash = require('connect-flash');

// during dev
winston.level = 'debug';


/*
 * Database query
 * Searches db for user that matches provided username
 */
var findUser = function (db, id, callback) {
    var cursor = db.collection('userInfo').find({username: id.username});
    var result = [];
    cursor.each(function (err, doc) {
        assert.equal(err, null);
        if (doc !== null) {
            result.push(doc);
        } else {
            callback(result);
        }
    });
};


// configure passport to use username and password authentication
passport.use(new LocalStrategy(
  function(username, password, done) {
        MongoClient.connect(mongoUrl, function (err, db) {
            assert.equal(null, err);
            findUser(db, {username:username, password:password}, function (result) {
                db.close();

                if (err) {
                    return done(err);
                }else if (result === []) {
                    winston.debug('Incorrect username');
                return done(null, false, { message: 'Incorrect username.' });
              }else if (password !== result.password) {
                    winston.debug('Incorrect password');
                return done(null, false, { message: 'Incorrect password.' }); // this is the line that causes error
              }
              return done(null, result);
            });
        });
  }
));
passport.serializeUser(function(user, done) {
  return done(null, user);
});
passport.deserializeUser(function(id, done) {
    MongoClient.connect(mongoUrl, function (err, db) {
        assert.equal(null, err);
        findUser(db, id, function (result) {
            db.close();
            return done(null, result);
        });
    });
});

app.configure(function() {
  app.use(express.static('public'));
  app.use(express.cookieParser());
  app.use(express.bodyParser());
  app.use(express.session({ secret: 'keyboard cat' }));
  app.use(passport.initialize());
  app.use(passport.session());
  app.use(app.router);
  app.use(flash());
});

/*
 * setup endpoints
  */
app.get('/', function(req, res){
    winston.debug('GET /');
  res.sendfile('views/index.html');
});
app.get('/success', function(req, res){
    winston.debug('GET /success');
  res.sendfile('views/success.html');
});
app.post('/login',
  passport.authenticate('local', { successRedirect: '/success',
                                   failureRedirect: '/',
                                   failureFlash: true })
);

// start server
http.listen(port, ipaddress, function(){
  winston.info('Listening on ' + ipaddress + ':' + port);
});

您需要将flash添加到express中,以便将其用作中间件

var flash = require('connect-flash');
...
app.use(flash());

编辑:这样做的原因是flash实际上并没有内置在Passport或Express中,它是由
connect flash
提供的一个单独的包。因此,您需要使用npm安装它,然后如上图所示将其包括在内。

即使这些行+添加到
package.json
npm install
。。同样的问题。用台词更新了问题。嗯,好吧。。。您是否尝试将
passReqToCallback
选项添加到您的localstrategy?似乎它修复了此人的错误:需要将
flash
中间件放在堆栈的更高位置,在
路由器之前,可能在passport之前。