使用req.session.xxx時出現 Cannot set property ‘xxxx‘ of undefined
問題如下
檢視app.js 程式碼發現引入的session模組 為灰色沒有使用
檢視express-session中的官方文件其中req.session使用案例為:
// Use the session middleware
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}))
// Access the session as req.session
app.get('/', function(req, res, next) {
if (req.session.views) {
req.session.views++
res.setHeader('Content-Type', 'text/html')
res.write('<p>views: ' + req.session.views + '</p>')
res.write('<p>expires in: ' + (req.session.cookie.maxAge / 1000) + 's</p>')
res.end()
} else {
req.session.views = 1
res.end('welcome to the session demo. refresh!')
}
})
發現沒有配置session的中介軟體
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}))
固配置中介軟體
// 配置session的中介軟體
app.use(session({ secret: 'secret key' }));
執行結果顯示正常:
相關文章
- BUG——AngularJS:Cannot set property ‘pic‘ of undefinedAngularJSUndefined
- Cannot set property 'innerHTML' of nullHTMLNull
- Cannot read property ‘aDataSort‘ of undefinedUndefined
- Cannot set property ‘type‘ of null(vue)NullVue
- js提示Cannot read property ‘replace‘ of undefinedJSUndefined
- echarts:Uncaught TypeError: Cannot read property '0' of undefinedEchartsErrorUndefined
- 前端報錯:cannot read property length of undefined前端Undefined
- 執行專案報錯Cannot read property 'styles' of undefinedUndefined
- Appium dmg 安裝:[TypeError: Cannot read property 'replace' of undefined]APPErrorUndefined
- [譯] 避免那些可惡的 "cannot read property of undefined" 錯誤Undefined
- vue 踩坑記錄 cannot read property xxx of undefinedVueUndefined
- Cannot set property ‘dataIndex‘ of undefined 大資料關係圖報錯,賦予的資料有重複,去重AIIndexUndefined大資料
- el-tree 報錯 TypeError: Cannot read property ‘setCheckedKeys‘ of undefined“ErrorUndefined
- vue 中引用echarts 初始化init undefind問題(Cannot read property ‘init‘ of undefined)VueEchartsUndefined
- [Vue warn]: Error in render: "TypeError: Cannot read property 'matched' of undefined" found in <App> at src/App.vueVueErrorUndefinedAPP
- vue-router.esm.js?fe87:2210 TypeError: Cannot read property ‘$createElement‘ of undefinedVueJSErrorUndefined
- 報錯xxxx of undefinedUndefined
- 開發中遇到的bug-Cannot set property ‘__VUE_DEVTOOLS_UID__‘ of nullVuedevUINull
- Need to set ‘serverTimezone‘ propertyServer
- Undefined property: stdClass::$column_name 怎麼解決?Undefined
- (排坑) Cannot create property 'key' on boolean 'true'Boolean
- AndroidStudio之自定義輸出包名報錯 Cannot set the value of read-only property 'outputFile' forAndroid
- cannot read prpperty ‘start‘ of undefined(問題記錄)Undefined
- vue+echarts報錯Cannot read property ‘init‘ of underfinedVueEcharts
- 裝完PHP後重起Apache的時候出現"Undefined symbols"錯誤(轉)PHPApacheUndefinedSymbol
- cannot set user id: Resource temporarily unavailableAI
- 當編譯AFNetworking 2.0時出現了Undefined symbols for architecture i386編譯UndefinedSymbol
- How to: Use Resources to Set Property Values in Web Server ControlsWebServer
- php執行出現Call to undefined function curl_init()PHPUndefinedFunction
- Centos7系統建立使用者時出現“useradd: user ‘xxxx‘ already exists”錯誤CentOS
- postfix時常提示出現關於set-uid的錯誤(轉)UI
- DEBUG : Eclipse Debug 時出現 Cannot connect to VM select failed錯誤EclipseAI
- 使用普通使用者set autotrace on報錯SP2-0618: Cannot find the Session IdentifierSessionIDE
- [Linux] mutt+msmtp傳送mail時出現瞭如下錯誤(msmtp: cannot locate host)LinuxAI
- Redis 中的 set 和 sorted set 如何使用,原始碼實現分析Redis原始碼
- su cannot set user id Resource temporarily unavailable故障解決AI
- su - oracle報錯su: cannot set user id: Resource temporarily unavailableOracleAI
- ORA-00265: instance recovery required, cannot set ARCHIVELOGUIHive