michaeldeMacBook-Pro:blog michael$ node app
express-session deprecated undefined resave option; provide resave option app.js:24:9
express-session deprecated undefined saveUninitialized option; provide saveUninitialized option app.js:24:9
Express server listening on port 3000
/Users/michael/blog/node_modules/mongodb/lib/mongodb/connection/base.js:246
throw message;
^
TypeError: Cannot read property 'length' of undefined
at processResults (/Users/michael/blog/node_modules/mongodb/lib/mongodb/db.js:1603:31)
at /Users/michael/blog/node_modules/mongodb/lib/mongodb/db.js:1641:20
at /Users/michael/blog/node_modules/mongodb/lib/mongodb/db.js:1179:7
at /Users/michael/blog/node_modules/mongodb/lib/mongodb/db.js:1912:9
at Server.Base._callHandler (/Users/michael/blog/node_modules/mongodb/lib/mongodb/connection/base.js:448:41)
at /Users/michael/blog/node_modules/mongodb/lib/mongodb/connection/server.js:481:18
at MongoReply.parseBody (/Users/michael/blog/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
at null.<anonymous> (/Users/michael/blog/node_modules/mongodb/lib/mongodb/connection/server.js:439:20)
at emit (events.js:107:17)
at null.<anonymous> (/Users/michael/blog/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:201:13)
到了注册这一步就遇到这样的问题 不知道如何解决
看的哪本书啊?
@chapgaga Node.js实战!
看看你register路由控制器的代码
@qianjiahao app.get(’/reg’,function(req,res){ res.render(‘reg’,{ title: ‘注册’, /user: req.session.user, success: req.flash(‘success’).toString(), error: req.flash(‘error’).toString()/ }); });
//app.post('/reg',checkNotLogin);
app.post('/reg',function(req, res){
var name = req.body.name,
password = req.body.password,
password_re = req.body['password_repeat'];
if(password_re != password){
req.flash('error','两次输入的密码不一致!');
return res.redirect('/reg');
}
var md5 = crypto.createHash('md5'),
password = md5.update(req.body.password).digest('hex');
var newUser = new User({
name: name,
password: password,
email:req.body.email
});
User.get(newUser.name,function(err,user){
if(user){
req.flash('error','用户已存在!');
return res.redirect('/reg');
}
newUser.save(function(err,user){
if(err){
req.flash('error',err);
return res.redirect('/reg');
}
req.session.user = user;
req.flash('success','注册成功!');
res.redirect('/');
});
});
});
前面的提示应该是你的session设置有问题,可以这样 <code> app.use(session({ secret: setting.cookieSecret, store: new MongoStore({ db: setting.db, }), resave: true, saveUninitialized: true })); </code>
@guoliangchen 你别按书上说的那个模块版本号, 用这个命令 npm install express-session connect-mongo -save 应用这两个模块的最新版本。
@rccoder 修改之后没用一样的报错,我把原项目的app.js考过来 删了暂时没遇到的 出现同样的问题
@qianjiahao npm之后出现同样的错误!我把原项目的app.js考过来 删了暂时没遇到的 出现同样的问题
@guoliangchen 注意express版本。另外请使用Mongo-Connect模块呗
我遇到了同样的问题搞了很久,是模块的版本没对上 { “name”: “N-blog”, “version”: “1.0.0”, “private”: true, “description”: “N-blog for express4.x”, “scripts”: { “start”: “node app” }, “dependencies”: { “express”: “4.10.2”, “body-parser”: “1.9.0”, “cookie-parser”: “1.3.3”, “morgan”: “1.3.1”, “serve-favicon”: “2.1.5”, “ejs”: “1.0.0”, “markdown”: “0.5.0”, “mongodb”: “1.4.15”, “express-session”: “1.9.1”, “connect-flash”: “0.1.1”, “connect-mongo”: “0.4.1”, “multer”: “0.1.6” } } 把package.json换成这个试试
严格照着书上的版本号来安装依赖库吧,应该跑得起来 这书现在看的话坑太多了:(
@DavidCai1993 什么书?
问题解决了么。求帮助呀。能给个QQ之类的么。
这书里的BUG很多,而且他的博客路由规则也不健壮。
@liuqiplus 1,使用最新的模块依赖 package.json中的 express-session: " * " , connect-mongo: " * " 2,注意MongoStore 首字母大写。我是这个问题 3,注意你的引入, connect-mongo 而不是 connect-mongodb