【问题标题】:Passport-Mongo, Cannot read property 'toLowerCase' of undefinedPassport-Mongo,无法读取未定义的属性“toLowerCase”
【发布时间】:2016-12-12 06:37:30
【问题描述】:

我正在尝试学习 Node Js,Passport 验证系统。我正在关注本教程:

http://code.tutsplus.com/tutorials/authenticating-nodejs-applications-with-passport--cms-21619

我在 npm 启动时收到错误“无法读取未定义的属性 'toLowerCase'”。

我不明白这个错误与什么有关,因为我在代码中看不到任何 toLowerCase 函数。

这是我的 package.json:

  {
    "name": "passport-mongo",
    "version": "0.0.1",
    "private": true,
    "scripts": {
    "start": "node ./bin/www"
   },
    "dependencies": {
      "express": "~4.2.0",
      "static-favicon": "~1.0.0",
      "morgan": "~1.0.0",
      "cookie-parser": "~1.0.1",
      "body-parser": "~1.0.0",
      "express-session": "~1.0.4",
      "debug": "~0.7.4",
      "jade": "~1.3.0",
      "passport": "~0.2.0",
      "passport-local": "~1.0.0",
      "mongodb": "^2.1.16",
      "mongoose": "^4.4.12",
      "bcrypt-nodejs" : "*",
      "connect-flash" : "*"
    }
     }

这里是 app.js

var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var dbConfig = require('./db.js');
var mongoose = require('mongoose');
// Connect to DB
mongoose.connect(dbConfig.url);

var app = express();

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

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// Configuring Passport
var passport = require('passport');
var expressSession = require('express-session');
// TODO - Why Do we need this key ?
app.use(expressSession({secret: 'mySecretKey'}));
app.use(passport.initialize());
app.use(passport.session());

 // Using the flash middleware provided by connect-flash to store messages in session
 // and displaying in templates
var flash = require('connect-flash');
app.use(flash());

// Initialize Passport
var initPassport = require('./passport/init');
initPassport(passport);

var routes = require('./routes/index')(passport);
app.use('/', routes);

/// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

module.exports = app;

【问题讨论】:

  • 你能显示你执行“npm start”时的完整堆栈跟踪吗?

标签: node.js mongodb mongoose exit passport.js


【解决方案1】:
C:\Projects!\Login3\passport-mongo>npm start

> passport-mongo@0.0.1 start C:\Projects!\Login3\passport-mongo
> node ./bin/www

C:\Projects!\Login3\passport-mongo\node_modules\express\lib\router\index.js:116
  var method = req.method.toLowerCase();
                         ^

TypeError: Cannot read property 'toLowerCase' of undefined
    at Function.proto.handle (C:\Projects!\Login3\passport-mongo\node_modules\express\lib\router\index.js:116:26)
    at router (C:\Projects!\Login3\passport-mongo\node_modules\express\lib\router\index.js:24:12)
    at Object.<anonymous> (C:\Projects!\Login3\passport-mongo\app.js:44:39)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (C:\Projects!\Login3\passport-mongo\bin\www:7:11)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)

npm ERR! Windows_NT 10.0.10586
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v4.4.4
npm ERR! npm  v2.15.1
npm ERR! code ELIFECYCLE
npm ERR! passport-mongo@0.0.1 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the passport-mongo@0.0.1 start script 'node ./bin/www'.
npm ERR! This is most likely a problem with the passport-mongo package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./bin/www
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs passport-mongo
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!
npm ERR!     npm owner ls passport-mongo
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     C:\Projects!\Login3\passport-mongo\npm-debug.log

C:\Projects!\Login3\passport-mongo>

【讨论】:

    猜你喜欢
    • 2021-11-28
    • 2020-07-23
    • 2020-06-24
    • 2018-10-23
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    相关资源
    最近更新 更多