使用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 read property ‘aDataSort‘ of undefinedUndefined
- Cannot set property 'innerHTML' of nullHTMLNull
- js提示Cannot read property ‘replace‘ of undefinedJSUndefined
- Cannot set property ‘type‘ of null(vue)NullVue
- echarts:Uncaught TypeError: Cannot read property '0' of undefinedEchartsErrorUndefined
- 前端報錯:cannot read property length of undefined前端Undefined
- vue 踩坑記錄 cannot read property xxx of undefinedVueUndefined
- 執行專案報錯Cannot read property 'styles' of undefinedUndefined
- el-tree 報錯 TypeError: Cannot read property ‘setCheckedKeys‘ of undefined“ErrorUndefined
- [譯] 避免那些可惡的 "cannot read property of undefined" 錯誤Undefined
- 報錯xxxx of undefinedUndefined
- Cannot set property ‘dataIndex‘ of undefined 大資料關係圖報錯,賦予的資料有重複,去重AIIndexUndefined大資料
- vue-router.esm.js?fe87:2210 TypeError: Cannot read property ‘$createElement‘ of undefinedVueJSErrorUndefined
- [Vue warn]: Error in render: "TypeError: Cannot read property 'matched' of undefined" found in <App> at src/App.vueVueErrorUndefinedAPP
- vue 中引用echarts 初始化init undefind問題(Cannot read property ‘init‘ of undefined)VueEchartsUndefined
- 開發中遇到的bug-Cannot set property ‘__VUE_DEVTOOLS_UID__‘ of nullVuedevUINull
- Need to set ‘serverTimezone‘ propertyServer
- AndroidStudio之自定義輸出包名報錯 Cannot set the value of read-only property 'outputFile' forAndroid
- (排坑) Cannot create property 'key' on boolean 'true'Boolean
- Centos7系統建立使用者時出現“useradd: user ‘xxxx‘ already exists”錯誤CentOS
- Undefined property: stdClass::$column_name 怎麼解決?Undefined
- cannot read prpperty ‘start‘ of undefined(問題記錄)Undefined
- vue+echarts報錯Cannot read property ‘init‘ of underfinedVueEcharts
- 開發中遇到的bug-Property or method “xxxx“ is not defined on the instance but referenced during render.
- 使用@Service註解出現No bean named 'xxxx' available]錯誤BeanAI
- php執行出現Call to undefined function curl_init()PHPUndefinedFunction
- Redis 中的 set 和 sorted set 如何使用,原始碼實現分析Redis原始碼
- Android Studio 出現“Cannot resolve symbol” 解決辦法AndroidSymbol
- IDEA出現Cannot resolve symbol “xxx”(無法解析符號)IdeaSymbol符號
- JAVA--set介面及其實現類的使用Java
- Python深入淺出property特性屬性Python
- vue-property-decorator使用指南Vue
- Vue.set與vue.$set的使用Vue
- 備忘錄:SQL SERVER2014 出現:“Cannot find one or more components”SQLServer
- STL使用篇__set
- 使用者名稱的格式xxxx......@xxxxs
- Server returns invalid timezone. Need to set ‘serverTimezone‘ property. Idea連線mysql報錯問題ServerIdeaMySql