在学习《第1章 一个简单的博客》遇到的问题
发布于 1个月前 作者 guoliangchen 564 次浏览 最后一次编辑是 21天前 来自 问答
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)
到了注册这一步就遇到这样的问题 不知道如何解决
12 回复

看的哪本书啊?

看看你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设置有问题,可以这样 app.use(session({ secret: setting.cookieSecret, store: new MongoStore({ db: setting.db, }), resave: true, saveUninitialized: true }));

@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换成这个试试

严格照着书上的版本号来安装依赖库吧,应该跑得起来 这书现在看的话坑太多了:(

回到顶部