I am in my first adventure with nodejs and mongodb on this tutorial "Rapid Prototyping Web Application tutorial with nodejs and mongodb" (safaribooksonline).
The error happens when I try to login, it was supposed to create a login entry with the first login attempt, but the following error is appearing:
**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)
The tutorial has its code on GIT : https://github.com/shorttompkins/movieq.git
Neither my code or the GIT code seems to work.
my controller.js file:
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('/');
}
});
},(...)
The following npm packages were used: npm install --save express express-session reqandlebars mongodb morgan cookie-parser body-parser method-override underscore
via LearningJava
No comments:
Post a Comment