带有nodejs和mongodb的快速原型Web应用程序教程,代码问题?

时间:2017-04-26 22:08:47

标签: node.js mongodb login

我是第一次使用nodejs和mongodb参与本教程“使用nodejs和mongodb进行快速原型开发Web应用程序教程”(safaribooksonline)。

当我尝试登录时发生错误,它应该在第一次登录尝试时创建一个登录条目,但出现以下错误:

**TypeError: Cannot read property 'collection' of null
at processLogin (/Users/brunofl/Documents/_webdev/rapid-prototypingWebApp/movieQ_start/server/controller.js:45:4)**
at Layer.handle [as handle_request] (/Users/brunofl/Documents/_webdev/rapid-prototypingWebApp/movieQ_start/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/brunofl/Documents/_webdev/rapid-prototypingWebApp/movieQ_start/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/brunofl/Documents/_webdev/rapid-prototypingWebApp/movieQ_start/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/brunofl/Documents/_webdev/rapid-prototypingWebApp/movieQ_start/node_modules/express/lib/router/layer.js:95:5)
at /Users/brunofl/Documents/_webdev/rapid-prototypingWebApp/movieQ_start/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/brunofl/Documents/_webdev/rapid-prototypingWebApp/movieQ_start/node_modules/express/lib/router/index.js:335:12)
at next (/Users/brunofl/Documents/_webdev/rapid-prototypingWebApp/movieQ_start/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/brunofl/Documents/_webdev/rapid-prototypingWebApp/movieQ_start/node_modules/express/lib/router/index.js:174:3)
at router (/Users/brunofl/Documents/_webdev/rapid-prototypingWebApp/movieQ_start/node_modules/express/lib/router/index.js:47:12)

本教程的代码在GIT上:https://github.com/shorttompkins/movieq.git

我的代码或GIT代码似乎都不起作用。

我的controller.js文件:

var client = require('./mongo_client'),
request = require('request');

module.exports = { //boilerplated endpoints
index: function (req, res) {
    if (!req.session.userId) {
        res.redirect('/login');
    } else {
        var filter = { // filters not watched movies
                'userId': req.session.userId,
                'watched': false
            },
            options = { //sorts by the descending release date order
                sort: [['release_date', 1]]
            };
        //starts using the mongoDB using '.db()' function that returns the connection
        //when you make a reference to collection or DB even if it doesn't exist mongoDB creates it for you
        client.db().collection('movies') //selects the collection
            .find(filter, {}, options)  //filters inside the collection
            .toArray(function (err, movies) {
                if (err) {
                    throw err;
                }
                res.render('index', {
                    'pageTitle': 'My Movies!!',
                    'movies': movies
                });
            });
    }
},
showLogin: function (req, res) {
    res.render('login');
},

processLogin: function (req, res) {
    // defines user objetc
    var userObj = {
        email: req.body.email, //because bodyParser npm package is used as middleware
        password: req.body.password
    };

    client
        .db()
        .collection('users') //find user on users collection
        .find(userObj, {}, {})
        .toArray(function (err, users) {
            if (users.length === 0) {
                client
                    .db()
                    .collection('users')
                    .insert(userObj, function (err, users) {//insert user as it was already registered
                        req.session.userId = users[0]._id; //associate user with id
                        res.redirect('/');
                    });
            } else {
                req.session.userId = users[0]._id;
                res.redirect('/');
            }
        });
},(...)

使用了以下npm包:     npm install --save express express-session reqandlebars mongodb morgan cookie-parser body-parser method-override下划线

1 个答案:

答案 0 :(得分:0)

嗯,仅供参考我问题得到了解决:

采取的行动: 要启动mongodb,我需要运行mongod --dbpath / path /

由于processLogin函数中的以下controller.js行出现问题,应用程序未进行登录:     " req.session.userId = users [0] ._ id;" 我确实把它改为:     req.session.userId = users._id; 并完成了应用程序开始工作。