2016-05-17 70 views
0

你好,我有諾德JS服務器項目 「connect.sid」,我試圖用這個教程 https://scotch.io/tutorials/easy-node-authentication-setup-and-local 與會議工作,我得到這個錯誤節點JS無法讀取屬性未定義

TypeError: Cannot read property 'connect.sid' of undefined 
    at Layer.session [as handle] (C:\Users\Erez\node\test\node_modules\express-session\index.js:115:32) 
    at trim_prefix (C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:226:17) 
    at C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:198:9 
    at Function.proto.process_params (C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:251:12) 
    at Context.next (C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:189:19) 
    at Context.actions.pass (C:\Users\Erez\node\test\node_modules\passport\lib\passport\context\http\actions.js:77:8) 
    at SessionStrategy.authenticate (C:\Users\Erez\node\test\node_modules\passport\lib\passport\strategies\session.js:67:10) 
    at attempt (C:\Users\Erez\node\test\node_modules\passport\lib\passport\middleware\authenticate.js:243:16) 
    at Passport.authenticate (C:\Users\Erez\node\test\node_modules\passport\lib\passport\middleware\authenticate.js:244:7) 
    at trim_prefix (C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:226:17) 
    at C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:198:9 
    at Function.proto.process_params (C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:251:12) 
    at next (C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:189:19) 
    at Passport.initialize (C:\Users\Erez\node\test\node_modules\passport\lib\passport\middleware\initialize.js:69:5) 
    at trim_prefix (C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:226:17) 
    at C:\Users\Erez\node\test\node_modules\express\lib\router\index.js:198:9 

這裏是我server.js

var express = require('express'); 
var app = express(); 
var mongoose = require('mongoose'); 
var passport = require('passport'); 
var flash = require('connect-flash'); 

var morgan = require('morgan'); 
var cookieParser = require('cookie-parser'); 
var bodyParser = require('body-parser'); 
var session = require('express-session'); 

var configDB = require('./config/database.js'); 

// configuration =============================================================== 
mongoose.connect(configDB.url); // connect to our database 

// required for passport 
app.use(session({ secret: 'iloveschocolate'})); // session secret 
app.use(passport.initialize()); 
app.use(passport.session()); // persistent login sessions 
app.use(flash()); // use connect-flash for flash messages stored in session 

app.use(bodyParser.urlencoded({ extended: true })); 
app.use(bodyParser.json()); 

var port = process.env.PORT || 3001; 


// middleware to use for all requests 
app.use(function(req, res, next) { 

    // Website you wish to allow to connect 
    res.setHeader('Access-Control-Allow-Origin', '*'); 
    // Request methods you wish to allow 
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); 
    // Request headers you wish to allow 
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); 
    // Set to true if you need the website to include cookies in the requests sent 
    // to the API (e.g. in case you use sessions) 
    res.setHeader('Access-Control-Allow-Credentials', true); 

    // do logging 
    next(); 
}); 

require('./Controllers/routes.js')(app, passport); 

app.listen(port); 
console.log('see whats happening on port ' + port); 

我已經嘗試了許多答案,並沒有幫助我 我有嘗試這個Cannot read property 'connect.sid' of undefined at Layer.session - ExpressJS sessionCannot read property 'connect.sid' of undefined at Layer.session - ExpressJS sessionnodejs express session error 也該文件有關connect - http://www.senchalabs.org/connect/session.html#session

當我刪除此行app.use(session({ secret: 'iloveschocolate',})); // session secret它的工作原理,但它所有的點使用這個會議?如果我是正確的

我用快遞4,所以我不能添加express.cookie解析器 感謝助理

+0

爲什麼是','後有ilovechoclate?只是好奇。 –

+0

這裏編輯錯誤 – Erez

+0

我很困惑。 'connect.sid'部分在哪裏? –

回答

1

也許是因爲你沒有使用你的cookieparser,前初始化()

app.use(cookieParser()); 
+0

會話數據沒有保存在cookie本身中,但是使用了cookie,所以我們必須在session()之前使用cookieParser()中間件。 –

0

嘗試會議之前放置cookieParser:

app.use(cookieParser()); 

    app.use(session(
     { 
     secret: 'appsecretkey', 
     resave: false, 
     saveUninitialized: true, 
     cookie: { 
      secure: true, 
      maxAge: new Date(Date.now() + 3600000) 
     } 
     })); 
相關問題