Build a Notes App using NodeJs, Express, MongoDB & Passport – CRUD
Today we are going to create a simple Notes taking Application using Nodejs, Express, MongoDB, and Passport. For templating we will use EJS and Bootstrap.
Improvement Ideas
- Notes Ritch Text Editor
- Notes Encryption
- Predictive Search
- Better RWD
- Add More Authentication Options
- Flash Messages
- Code Improvement Overall
- Create API to be used on Front End
- Add More UI Interactivity
Mongoose ^7.0.0 Update
Please replace the following code in auth.js
// REPLACE THIS passport.deserializeUser(function (id, done) { User.findById(id, function (err, user) { done(err, user); }); }); // TO THIS passport.deserializeUser(async (id, done) => { try { const user = await User.findById(id); done(null, user); } catch (err) { done(err, null); } });
In dashboardController.js please replace the full Get Dashboard Route
/** * GET / * Dashboard */ exports.dashboard = async (req, res) => { let perPage = 12; let page = req.query.page || 1; const locals = { title: "Dashboard", description: "Free NodeJS Notes App.", }; try { // Mongoose "^7.0.0 Update const notes = await Note.aggregate([ { $sort: { updatedAt: -1 } }, { $match: { user: new mongoose.Types.ObjectId(req.user.id) } }, { $project: { title: { $substr: ["$title", 0, 30] }, body: { $substr: ["$body", 0, 100] }, }, }, ]) .skip(perPage * page - perPage) .limit(perPage) .exec(); const count = await Note.count(); res.render('dashboard/index', { userName: req.user.firstName, locals, notes, layout: "../views/layouts/dashboard", current: page, pages: Math.ceil(count / perPage) }); } catch (error) { console.log(error); } };
Credit:
- Photo by Diego PH on Unsplash
- Pagination EJS – https://evdokimovm.github.io/
- Passport SerializeUser – StackOverflow
More Resources:
- Display NodeJs Flash Messages using ESJ
- NodeJs Free Hosting
- Query MongoDB with Custom URL Parameters
Hello raddy i have watched your Video step by step and wrote every code line with you
but the problem is: when i tried to sign in to google account to view the dashboard and see the changes it gave me this error: MongoServerError: E11000 duplicate key error collection: test.users index: googleid_1 dup key: { googleid: null }
at InsertOneOperation.execute (C:\Users\Admin\OneDrive\Documents\NodeJs2\node_modules\mongodb\lib\operations\insert.js:51:19)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async tryOperation (C:\Users\Admin\OneDrive\Documents\NodeJs2\node_modules\mongodb\lib\operations\execute_operation.js:207:20)
at async executeOperation (C:\Users\Admin\OneDrive\Documents\NodeJs2\node_modules\mongodb\lib\operations\execute_operation.js:75:16)
at async Collection.insertOne (C:\Users\Admin\OneDrive\Documents\NodeJs2\node_modules\mongodb\lib\collection.js:157:16)
even if i go to mongoDB website and delete the sessions, nothing changes.
what should i do ?
The error suggest that you already have a record with googleid where the value is null. If you delete that record, it shouldn’t come up. But the real question here is why are you getting GoogleId of null? Any chance you can link your Github repo so I can test?