按着《NodeJS开发指南》里的第五章建立microblog的例子操作,使用node.js 的express框架配置将session存储到mongodb时出错:TypeError: Cannot read property 'Store' of undefined。

加载connect-mongo模块的写法为:

//加载模块

var MongoStore= require("connect-mongo");

//配置express session

app.use(express.session({

   secret:setting.cookieSecret,

   db: seeting.db

})); 

启动程序报错:TypeError: Cannot read property 'Store' of undefined

网上有人修改如下之后重启是OK的:

With express:

    var express = require('express');
var MongoStore = require('connect-mongo')(express); app.use(express.session({
secret: settings.cookie_secret,
store: new MongoStore({
db: settings.db
})
}));

但是我修改之后报如下错误:

Error: Most middleware (like session) is no longer bundled with Express and must
be installed separately. Please see https://github.com/senchalabs/connect#middl
eware.
at Function.Object.defineProperty.get (E:\-Study\NoteJS\microblog\node_m
odules\express\lib\express.js::)
at module.exports (E:\-Study\NoteJS\microblog\node_modules\connect-mongo
\lib\connect-mongo.js::)
at Object.<anonymous> (E:\-Study\NoteJS\microblog\app.js::)
at Module._compile (module.js::)
at Object.Module._extensions..js (module.js::)
at Module.load (module.js::)
at Function.Module._load (module.js::)
at Module.require (module.js::)
at require (module.js::)

后来修改回原来的写法,提示找不到“Store”,然后就到目录“\node_modules\connect-mongo”下查看Readme.md文档,上面提示:

With express4:

    var session    = require('express-session');
var MongoStore = require('connect-mongo')(session); app.use(session({
secret: settings.cookie_secret,
store: new MongoStore({
db : settings.db,
})
})); With express<: var express = require('express');
var MongoStore = require('connect-mongo')(express); app.use(express.session({
secret: settings.cookie_secret,
store: new MongoStore({
db: settings.db
})
})); With connect: var connect = require('connect');
var MongoStore = require('connect-mongo')(connect);

因为本机Express的版本是4.0,所以修改成用第一种方式引用session,同时修改目录下的package.json文件,添加对“express-session”的依赖如下:

{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"express": "~4.0.0",
"static-favicon": "~1.0.0",
"morgan": "~1.0.0",
"cookie-parser": "~1.0.1",
"body-parser": "~1.0.0",
"debug": "~0.7.4",
"ejs": "~0.8.5",
"mongodb": ">= 0.9.9",
"connect-mongo": ">= 0.1.7",
"express-session": "~1.0.4"
}
}

然后在命令行下进入到当前目录,执行npm install就默认安装了express-session模块了。

之后再执行npm start启动服务,问题就解决了。

NodeJS - Express 4.0错误:Cannot read property 'Store' of undefined的更多相关文章

  1. NodeJS - Express 4.0下使用app.dynamicHelpers错误

    在NodeJS - Express 4.0下使用app.dynamicHelpers发生错误: app.dynamicHelpers({ ^ TypeError: Object function (r ...

  2. NodeJS -Express 4.0 用include取代partial

    在Express 4.0 下按如下方法设置: (1)运行cmd 输入:npm install express-partials -g (2)下载成功后.在app.js 中引用此插件   var par ...

  3. NodeJS - Express 3.0下ejs模板使用 partial展现 片段视图

    如果你也在看Node.js开发指南,如果你也在一步一步实现 microBlog 项目!也许你会遇到本文提到的问题,如果你用的是Express 3.0 本书实例背景是 Express 2.0 而如今升级 ...

  4. jquery错误: Cannot read property ‘msie’ of undefined

    背景 Web application, 引用了jquery 1.10.2和fancybox 1.3.4 现象 访问页面遭遇Cannot read property ‘msie’ of undefine ...

  5. Visual Studio 2013 Ultimate & IIS Express 8.0 错误 [iisexpress.exe”已退出,返回值为 -1073741816 (0xc0000008)] 解决方法

    1. 开发环境 Visual Studio 2013 Ultimate IIS 8.0 Express 2. 错误信息 错误提示:iisexpress.exe”已退出,返回值为 -1073741816 ...

  6. NodeJS - Express4.0错误:Cannot read property &amp;#39;Store&amp;#39; of undefined

    Express在使用mongodb的时候app配置出错 //settings.js module.exports={ cookieSecret:"xxxx", db:"d ...

  7. ExtJS错误解决 Cannot read property 'on' of undefined

    背景 用ExtJS新写了一个功能,运行时控制台打印错误Cannot read property 'on' of undefined,出错代码位置是Ext.define.bindStoreListene ...

  8. 解决vue2.0路由 TypeError: Cannot read property 'matched' of undefined 的错误问题

    刚开始使用vue-router2.0,虽然也用了vux,用起来却发现一个问题--具体如下: 正常情况下使用脚手架跑完之后,然后修改源项目,首先在main.js入口里把该import进去的vuex,vu ...

  9. Nodejs开发框架Express3.0开发手记–从零开始

    转载请注明出处: http://blog.fens.me/nodejs-express3/ 程序代码已经上传到github有需要的同学,自行下载. https://github.com/bsspiri ...

随机推荐

  1. ETL流程介绍及常用实现方法

    ETL是英文Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取(extract).转换(transform).加载(load)至目的端的过程.常见于数据仓库开发中将数 ...

  2. os模块 与 sys模块

    os模块 os模块是与操作系统交互的一个接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工 ...

  3. Centos7.2下OpenVPN 环境完整部署记录

    关于OpenVPN的有关介绍及为何使用OpenVPN在此就不做赘述了,下面直接记录Centos7.2系统下部署OpenVPN环境的操作过程: 1) 先将本机的yum换成阿里云的yum源 [root@t ...

  4. BugPhobia回顾篇章:团队Beta 阶段工作分析

    0x00:序言 1 universe, 9 planets, 204 countries,809 islands, 7 seas, and i had the privilege to meet yo ...

  5. 同步手绘板——PC端实现画板

    同步显示上设想通过bitmap传值再在web端显示,查阅资料发现有点难以实现,所以在web也生成一个画板,实现与android端类似功能,由android传递路径集合到web端显示.

  6. [2017BUAA软工]第0次博客作业

    第一部分:结缘计算机 1.你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢? 当初选择计算机专业作为自己报考大学的第一志愿,主要是看重了市场对于计算机行业人士的巨大需求,同时也感慨于计算机行 ...

  7. org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unexpected failure during bean definition parsing Offending resource: class path resource [applicationC

    这个错误是 org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration proble ...

  8. sublime text3修改默认配置文件是失败的解决方法

    如果你修改sublime text3的默认配置文件Preferences.sublime-settings失败,现实的错误信息如下图: 其实根据提示信息就好找问题出在哪里了:权限 要想成功的修改默认配 ...

  9. 牛客OI周赛7-普及组

    https://ac.nowcoder.com/acm/contest/372#question A.救救猫咪 #include <bits/stdc++.h> using namespa ...

  10. PAT 甲级 1135 Is It A Red-Black Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640 There is a kind of bal ...