Fetch Data From MongoDB: Node.js

What’s the use of the data in database, if we can’t fetch it and display on a web page! Today’s video tutorial concentrates on fetching data out of our mongoDB collection and displaying it on the web page.

fetch-data-from-mongoDB-nodejs

Related Read: Save data To MongoDB: Node.js

Route to display registered users
app.js

1
2
3
4
5
6
app.get('/view', function(req, res){
user.find({}, function(err, docs){
if(err) res.json(err);
else    res.render('index', {users: docs});
});
});

Once the user visits /view route, she will be presented with all the registered users information. Inside /view route, we fetch all the user information by using mongoDB’s find() method. find() takes 2 parameters – first parameter is an object with condition, second parameter is a callback method. If we pass in empty object as first parameter, it means, fetch all the data – similar to SELECT * in sql.

If find() could successfully fetch the data out of mongoDB collection, it renders index.jade file and passes an object to it – which contains information of all the fetched users.

Template To Display User Information
view/index.jade

1
2
3
ul
each user in users
 li #{user.name}: #{user.age}: #{user._id}

Here we loop through users object, fetch individual user information, and display as a list item.

Fetch Data From MongoDB: Node.js


[youtube https://www.youtube.com/watch?v=U0zK-Nb2vn8]

YouTube Link: https://www.youtube.com/watch?v=U0zK-Nb2vn8 [Watch the Video In Full Screen.]



Note: Since email address has been made as _id, each registered user will have a unique email address associated with her.

Fetch individual user information
app.js

1
2
3
4
5
6
app.get('/view', function(req, res){
user.find({_id: '[email protected]'}, function(err, docs){
if(err) res.json(err);
else    res.render('index', {users: docs});
});
});

Observe the find() methods first parameter. Now we are passing _id to it, which means, it has to find and retrieve only the information related to the user with that _id.

Full Code To Insert And Fetch Data into/from MongoDB
app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var express = require('express');
var http = require('http');
var path = require('path');
var mongoose = require('mongoose');
 
var app = express();
 
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
 
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
 
mongoose.connect('mongodb://localhost/Company');
 
var Schema = new mongoose.Schema({
_id    : String,
name: String,
age   : Number
});
 
var user = mongoose.model('emp', Schema);
 
app.get('/view', function(req, res){
user.find({}, function(err, docs){
if(err) res.json(err);
else    res.render('index', {users: docs});
});
});
 
app.post('/new', function(req, res){
new user({
_id    : req.body.email,
name: req.body.name,
age   : req.body.age
}).save(function(err, doc){
if(err) res.json(err);
else    res.redirect('/view');
});
});
 
var server = http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

To make this work, go to Save data To MongoDB: Node.js tutorial page and copy the index.html page code and create one for your project and put it inside public directory. Now you can directly access root(html form), enter user information and submit, which redirects to /view route, wherein you get the information of all the registered users.

Note: Make sure MongoDB server is running and turned on, when you start Node.js server and access your application from web browser.

Related Read: MongoDB – Getting Started Guide

Save data To MongoDB: Node.js

After learning to connect our Node.js application to MongoDB, lets see how we could insert data into our database collection.

insert-into-mongoDB-nodejs

Note:
Save() is a higher lever method. It checks to see if the object we’re inserting is already present in our collection, by checking the _id value we’re inserting. _id field(which is a primary key in MongoDB document)
– If there is no matching _id field in a document, it’ll call insert method and insert the data. And if the data being entered has no _id value, insert() method will create _id value for us, and insert the data into the collection.
– If _id matches to any document in our collection, it’ll use update() instead to update the previously present data with the new data entered by the user.

Form To Facilitate User To Enter Her Details ..or Registration Form
public/index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< !DOCTYPE html>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
 <form action="/new" method="POST">
<label for="email">Email: </label>
  <input type="email" name="email" /><br />
<label for="name">Name: </label>
  <input type="text" name="name" /><br />
<label for="age">Age: </label>
  <input type="number" name="age" /><br />
 <input type="submit"/>
</form>
</body>
</html>

Here we have 3 input fields(for email, name and age) and a submit button. Since we’re posting these data to /new (in the action field of the form), we need to create /new route.

Route To Process User Entries
app.js

1
2
3
4
5
6
7
8
9
10
app.post('/new', function(req, res){
new user({
_id    : req.body.email,
name: req.body.name,
age   : req.body.age
}).save(function(err, doc){
if(err) res.json(err);
else    res.send('Successfully inserted!');
});
});

By using bodyParser() middleware, we get the data from the form, parse it and assign the values to corresponding keys, by creating a new user.

Note: user is a model object in our example. We also chain save method with new user object. Save() method takes a callback method as argument. The callback method takes 2 arguments – error and document. We check, if there are any errors while inserting the data, if so, we output the error object on to the screen. If the insertion was successful, we display “Successfully inserted!” message.

Inserting data To MongoDB: Node.js


[youtube https://www.youtube.com/watch?v=uZqwHfNIf8M]

YouTube Link: https://www.youtube.com/watch?v=uZqwHfNIf8M [Watch the Video In Full Screen.]



Related Read:
number Input Type: HTML5
Create and Insert Documents: MongoDB
MongoDB Tutorial List

Full Code
app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var express = require('express');
var http = require('http');
var path = require('path');
var mongoose = require('mongoose');
 
var app = express();
 
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
 
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
 
mongoose.connect('mongodb://localhost/Company');
 
var Schema = new mongoose.Schema({
_id    : String,
name: String,
age   : Number
});
 
var user = mongoose.model('emp', Schema);
 
app.post('/new', function(req, res){
new user({
_id    : req.body.email,
name: req.body.name,
age   : req.body.age
}).save(function(err, doc){
if(err) res.json(err);
else    res.send('Successfully inserted!');
});
});
 
 
 
var server = http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

Here we’ve shown the configurations, middleware, mongoose connection, schema, model object, routes and out application server.

Connecting To MongoDB Using Mongoose: Node.js

In today’s video tutorial lets learn to connect to MongoDB server from our Node.js application using Mongoose module.

node-mongoose-mongoDB

Mongoose Module
Mongoose provides a straight-forward, schema-based solution to modeling your application data and includes built-in type casting, validation, query building, business logic hooks and more..

MongoDB
MongoDB is one of the leading NoSQL database. Know more about it at MongoDB – Getting Started Guide

Related Read: MongoDB Tutorial List

Listing Mongoose As A Dependency
package.json

1
2
3
4
5
6
7
8
9
10
11
12
13
{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "nodemon app.js"
  },
  "dependencies": {
"express": "*",
"nodemon": "*",
"mongoose": "*"
  }
}

list the mongoose module as a dependency for your Node.js application

Install the dependencies

1
npm install

This looks for all the dependencies listed in package.json file and installs it. Make sure that your system is connected to the internet.

Connecting to MongoDB
app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var mongoose = require('mongoose');
 
app.use(express.bodyParser());
app.use(express.methodOverride());
 
mongoose.connect('mongodb://localhost/Company');
 
var mySchema = new mongoose.Schema({
_id    : String,
name: String,
age   : Number
});
 
var user = mongoose.model('emp', mySchema);

We have to first include mongoose module into our application. We also need the help of bodyParser() and methodOverride() middlewares in order to parse the form variables and override method to make PUT and DELETE methods work.

Next we connect to mongobd using connect method of mongoose module. Here we also specify the mongoDB database name to which our application will be connecting to. If the database is not already present, this will create one for us.

Next, we write a schema definition for our collection. Mongoose uses schema for validation of user entered data. By specifying the data type for our field we can make sure user entries are validated against the data types we have mentioned in our schema.

Some valid Schema Types, supported by Mongoose
String
Number
Date
Buffer
Boolean
Mixed
Objectid
Array

Mongoose Model
A model in mongoose is an object that gives us easy access to a named collection, allowing us to query the collection and use the schema to validate any document we save to that collection.

Connecting To MongoDB Using Mongoose: Node.js


[youtube https://www.youtube.com/watch?v=93APwwh35PA]

YouTube Link: https://www.youtube.com/watch?v=93APwwh35PA [Watch the Video In Full Screen.]



Note 1: In above example code, we are creating a collection called emp and binding it with the schema mySchema

In above example
Database name: Company
Collection/table name: emp

Note 2: Make sure MongoDB server is up and running, before you execute your Node.js application.

Checking If your application succesfully connected to MongoDB Server!
app.js

1
2
3
4
5
var db = mongoose.connection('mongodb://localhost/Company');
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
  // all your database operations(CRUD) here 
});

Here by using on method we check if we could successfully connect to our mongoDB server. If there was any error, we output the error on to the screen. If the connection is successful, we’ll continue with our CRUD operations on the database tables.

Validating User Request: Node.js

Validating user requests is one of the key elements of a web application, and is critical for its performance.

validating-user-request

In this tutorial, we validate the user request against elements present inside an array. In real-time web applications, the request is validated against database values.

Validating User Request In Express: Node.js
app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var names = [
 "Satish",
 "Kiran",
 "Sunitha",
 "Jyothi"
];
 
app.param('username', function(req, res, next, username){
var flag = parseInt(names.indexOf(username), 10);
 
if(flag >= 0)
 next();
else 
 res.end("No Such User!");
});
 
app.get('/user/:username', function(req, res){
res.send("Viewing user: "+req.params.username);
});

when the user requests for data via the route /user/someUsername we check if the user is actually present. If he is present, we’ll serve the data or else we’ll send No Such User! message to the browser.

To keep the routes clean, we shift the code to app.param First parameter indicates to which route the app.param is bound to. The callback method takes a couple of arguments – request, response, next and the username the user has requested.

we make use of indexOf() method to check if the requested username is actually present in our array. If the element is present in the array, indexOf() returns its position or else it returns -1.
If it returns 0 or any other positive value, then call next() to pass the control to the next layer of execution or else, display No Such User! and end the response.

Validating User Request: Node.js


[youtube https://www.youtube.com/watch?v=Bug_P3lxhfA]

YouTube Link: https://www.youtube.com/watch?v=Bug_P3lxhfA [Watch the Video In Full Screen.]



Note: Usually if you retrieve data out of a MongoDB server, the data will be present in the form of object( {key: value} pair ).

Validating User Request In Express: Node.js
app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
var names = [
 {
"id"        :  1,
"name"   :  "Apple",
"product": "iPhone"
},
 {
"id"        :  2,
"name"   :  "Google",
"product": "Nexus"
},
 {
"id"        :  3,
"name"   :  "Technotip",
"product": "Education"
},
 {
"id"        :  4,
"name"   :  "Microsoft",
"product":  "Nokia Lumia"
}
];
var flag = undefined;
app.param('id', function(req, res, next, id){
 
for(var i = 0; i < names.length; i++ )
 if(names[i].id == id)
   flag = "<b>Company: "+names[i].name+
            "<br /><b>Product: </b>"+names[i].product;
 
if(flag != undefined)
  next();
else
        res.end("No Such User!");
});
 
app.get('/user/:id', function(req, res){
res.send(flag);
});

Here we have an array of objects. Once the user requests company information using company id(/user/:id), we check through each object’s id and if it matches we call next() or else send No Such User! to the browser.

some output
/user/0
No Such User!

/user/1
Company: Apple
Product: iPhone

/user/2
Company: Google
Product: Nexus

/user/4
Company: Microsoft
Product: Nokia Lumia

Home Work Combine today’s learning with Error handling and write complete code for user request validation as well as error handling using Error object.

Error Handling In Express: Node.js

User inputs are very unpredictable on the web. So error handling becomes very important while building any real-time web application.

error-handling-in-express-nodejs

Express facilitates very good support for error handling, with its connect middleware.

Page Not Found Error Handling In Express: Node.js
app.js

1
2
3
4
5
6
7
8
9
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req, res){
res.send(404, "Not found :-(");
});
 
app.get('/', function(req, res){
res.send("Homepage!");
});

When the user visits root of the website ( / ), a message called “Homepage!” is displayed to the user. But what if he tries to access a URL route which is not defined Ex: /user It displays Cannot GET /users message. We can set up a separate middleware to deal with situations like this. When we encounter a URL which doesn’t have a custom route definition, it’ll look for the file in the public directory, if the file is not found, it’ll look through our final middleware and executes whatever is present inside it.

User Not Found Error Handling In Express: Node.js
app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
app.use(function(err, req, res, next){
res.send(404, err.message);
next();
});
 
app.get('/user/:username', function(req, res, next){
if(req.params.username === 'kiran')
{
var err = new Error('User Not Found');
next(err);
}
res.send(req.params.username);
});

In real world application, we need to validate the user request before trying to serve them data. Here, if the user requests /user/kiran we’ll let the user know that the user kiran is not present in our database. So we setup a custom error handling middleware for situations like this. Here we create an error object and set a message, and pass this error object as parameter to next. call to next passes the control to the middleware which matches its signature, and executes whatever is present inside the middleware. [ Note the presence of err object inside the middleware callback method. ]

Error Handling In Express: Node.js


[youtube https://www.youtube.com/watch?v=noBowQTXSS4]

YouTube Link: https://www.youtube.com/watch?v=noBowQTXSS4 [Watch the Video In Full Screen.]



Note: Error handling can significantly increase the performance of your web application, due to the fact that your application need not figure out what to do next, in case of an error. You explicitly write error handling code for most common situations and the application behaves in a predictable manner.

Call to next() passes the execution control to the next level.