Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSimon Amz
4,606 PointsError implementing "connect-mongo"
Hi,
I installed connect-mongo with npm, and when I launch 'nodemon', I got an error message, saying : Error: Cannot find module 'connect-mongo'
Indeed, I have well required the module but I got this error message. Besides, I've done the DB connection in an other file (db.js) which I required to in app.js, before requiring express-session opr connect-mongo.
I don't know where the issue comes from.
Thanks for your help
6 Answers
Simon Amz
4,606 PointsOf course, this is how I got in db.js: const mongoose = require('mongoose') const User = require('./users');
// mongodb connection mongoose.connect('mongodb://localhost:27017/StartPitch', {useMongoClient: true}); mongoose.set('debug', true); var db = mongoose.connection; // mongodb error db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() { console.log('Connected to mongoDB') });
module.exports = db;
And here is what I got in app.js:
var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var db = require('./app_server/models/db'); var session = require('express-session'); var index = require('./app_server/routes/index'); var MongoStore = require('connect-mongo')(session); var app = express();
// use sessions for tracking app.use(session({ secret: 'Hine lo yanum velo yishan', resave: true, saveUninitialized: false, store: new MongoStore({ mongooseConnection: db }) }))
// make user ID available in templates app.use(function (req, res, next) { res.locals.currentUser = req.session.userId; next(); })
// view engine setup app.set('views', path.join(__dirname, 'app_server', 'views')); app.set('view engine', 'ejs');
ETC..
and in the shell here is what i got:
module.js:557 throw err; ^
Error: Cannot find module 'connect-mongo' at Function.Module._resolveFilename (module.js:555:15) at Function.Module._load (module.js:482:25) at Module.require (module.js:604:17) at require (internal/module.js:11:18) at Object.<anonymous> (/Users/simonamzalag/Programmation/nodejs/MEAN_Intro/StartPitch/app.js:10:18) at Module._compile (module.js:660:30) at Object.Module._extensions..js (module.js:671:10) at Module.load (module.js:573:32) at tryModuleLoad (module.js:513:12) at Function.Module._load (module.js:505:3) at Module.require (module.js:604:17) at require (internal/module.js:11:18) at Object.<anonymous> (/Users/simonamzalag/Programmation/nodejs/MEAN_Intro/StartPitch/bin/www:7:11) at Module._compile (module.js:660:30) at Object.Module._extensions..js (module.js:671:10) at Module.load (module.js:573:32) [nodemon] app crashed - waiting for file changes before starting...
Kevin Becerra
14,243 Pointsnpm install connect-mongodb-session --save
try running this in your terminal
Simon Amz
4,606 PointsI try it and the error doesn't appear anymore. Thanks for that. However when I logged in and I check in the database the collections I still have 'users' only.
Here is the new code: var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var db = require('./app_server/models/db'); var session = require('express-session'); var index = require('./app_server/routes/index'); var MongoDBStore = require('connect-mongodb-session')(session); var app = express();
// use sessions for tracking app.use(session({ secret: 'This is a secret', resave: true, saveUninitialized: false, store: new MongoDBStore({ mongooseConnection: db }) }))
For your info all other features were working perfectly.
Adam Beer
11,314 PointsHi Simon! Your code worked so far? May be upgrade the version of npm and mongo-session.
Adam Beer
11,314 PointsTry it add semicolon after the app.use(session({}))
Adam Beer
11,314 PointsI think I found it. Check you code in app.js. The order is wrong first step //mongodb connect, second step //use sessions for tracking logins. Please fixed in your the code. Your code is working now?
Jesus Mendoza
23,289 PointsJesus Mendoza
23,289 PointsHi Simono,
Can you show us your code?