user3183411
user3183411

Reputation: 335

Unable to insert record in MongoDB

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

Here is my posting route

router.post('/', 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");

        }

        db.close();
        res.render('index', {
          title: 'Welcome!!!',
          pageName: 'index'
        });

      });

    }

    db.close();

  });

});

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.
/Users/bberrelez/Projects/Personal/WebApp/illcomplacent/node_modules/mongodb/lib/utils.js:98
    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.

Upvotes: 0

Views: 1821

Answers (3)

ElPedro
ElPedro

Reputation: 586

I had something similar using Django where the driver converted my collection name to be all lower case. It may be that node.js is doing the same. Try checking in the Mongo shell to see if your code has created a blogpost collection and inserted the documents there instead of in blogPost.

Upvotes: 0

abdulbari
abdulbari

Reputation: 6242

You have given res as the parameter in insert callback function that's why you are getting this.

And local rest don't have render function.

Hope this will work for you.

router.post('/', 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, result) {// change `res` with any other vaiable

        if (err) {

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

          console.log("insert successful");

        }

        db.close();
        res.render('index', {
          title: 'Welcome!!!',
          pageName: 'index'
        });

      });

    }

    db.close();
  });
});

Upvotes: 1

Wake
Wake

Reputation: 1696

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) {

to

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)?

Upvotes: 1

Related Questions