Hacktoberfest — Second Pull Request

The second issue I worked on is to fix a bug for a personal website that can be adopted by anyone as a template. The website link is as below:


Issue and Pull Request

The issue link is posted here:

In order to use the website the user needs to sign up for a new account then login. The issue is that when displaying the user profile page, the “Visited Pages Count” is empty, as shown below.

Image for post

The project was developed using Node.js, and using MongoDB to persist user info and visit records. Each time a user visit a page, the record is saved in the “visits” collection of the database by calling function mongo.saveRecord. A typical processes is shown below:

let visit = {ip: req.ip, date: date.getTime(), user: req.session.user, page: "about"};mongo.saveRecord('visits', visit);

For retrieving data from the database, below aggregate function is called to return a json object of visited pages summary:

visits.aggregate([{$match: {user: user}}, {$group: {_id: "$page", count: {$sum: 1}}}, {$sort: {count: -1}}], (err2, results)=>{if(err2){console.log(err2); callback(false)}else{let visitedPages = {};results.forEach(page => {visitedPages[page._id] = page.count})info.visitedPages = visitedPages;callback(null, info);}

The aggregation relies on the “page” key value pair of the records to count the number of visits. However, after looking through the code, a function related to login did not have a page element in the declaration of json object. This caused null values of _id for the above aggregate function inside the group condition, which caused data retrieval issue. To fix this issue, I added the page element to the login processes before persisting record, which is shown below:

Image for post

I also moved the mongo.saveRecord inside the code block so only if the user exists in the system the visit record counts. The pull request link is as below:

The pull request got merged quickly. However, the maintainer believes the records should be saved no matter the user exists or not, so he moved the function outside like below:

Image for post


In order to debug for this project, I had to set up a .env configuration file with my own MongoDB account, password and port. The second pull request took me much longer to complete compared to the first one although the change is really small. Most of time spent was to read through the code to understand logic of different functions and how they are connected. For the third and fourth pull request, I would like to try some issues with more changes needed.

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store