user3183411 user3183411 - 2 years ago 112
Node.js Question

Unable to insert record in MongoDB

Can some help me understand why I cannot insert anything into my mongoDB?

Here is my posting route'/', function(req, res, next) {

console.log("i am posting data!");

var MongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017/blog';

MongoClient.connect(url, function(err, db) {

if (err) {

console.log('Error:', err);

} else {

console.log('connected to db');

var collection = db.collection('blogPost');

var blogPost = {

title: req.body.title,
status: req.body.status,
category: req.body.category,
tags: req.body.tags,
content: req.body.content,
createDate: new Date()


console.log("posting data:", blogPost);

collection.insert(blogPost, function(err, res) {

if (err) {

console.log('Error on Insert:', err);
} else {

console.log("insert successful");


res.render('index', {
title: 'Welcome!!!',
pageName: 'index'






Here is what I am seeing in the console log.

GET /js/bootstrap-multiselect.js 200 16.638 ms - 66344
i am posting data!
connected to db
posting data: { title: 'test',
status: 'A',
category: 'excerpt',
tags: [ 'life', 'teaching' ],
content: 'Test from app',
createDate: Sun Sep 04 2016 23:46:18 GMT-0600 (MDT) }
insert successful
Sun Sep 04 2016 23:46:18 GMT-0600 (MDT): Node server stopped.
process.nextTick(function() { throw err; });

TypeError: res.render is not a function
at /Users/bberrelez/Projects/Personal/WebApp/illcomplacent/routes/index.js:94:13
at /Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/collection.js:523:5
at /Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/collection.js:701:5
at handleCallback (/Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/utils.js:96:12)
at executeCommands (/Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/bulk/ordered.js:405:12)
at resultHandler (/Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/bulk/ordered.js:433:5)
at /Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/node_modules/mongodb-core/lib/connection/pool.js:436:18
at nextTickCallbackWith0Args (node.js:420:9)
at process._tickCallback (node.js:349:13)
Brians-MacBook-Pro:illcomplacent bberrelez$

The thing is, it's not even inserting into the MongoDB. I can insert data just fine from the command line but, that is not what I am needing to do. I need to insert this data from the application.

Answer Source

You've used the same variable name, res, twice. By the time you get to res.render, res is locally scoped to your callback and it's referring to the result from you insert, not your response object.

To solve that you can simple rename your insert result. Try changing:

collection.insert(blogPost, function(err, res) {


collection.insert(blogPost, function(err, result) {

That doesn't explain why your data isn't inserted, but it looks like your data should be there. Are you sure you're looking in the right database (blog) and collection (blogPost)?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download