user3413812
user3413812

Reputation:

Nodejs controller not working properly

router.get('/roomlist', function(req, res) {
    var db = req.db;
    var collection = db.get('roomlist');
    collection.find(function(e,docs){
        res.render('roomlist', {
            "roomlist" : docs
        });
    });
});

The above controller is used to get list of rooms under a table "ROOMLIST". I need to get the details. I need to display all the roomname in the table ROOMLIST.

Upvotes: 0

Views: 273

Answers (2)

riser101
riser101

Reputation: 650

You should use find method of mongodb to get the documents from a collection. And then use cursor to iterate through the documents.

var getRoomlist = function(db, callback) {
   var cursor =db.collection('roomlist').find( );
   cursor.each(function(err, doc) {
      assert.equal(err, null);
      if (doc != null) {
         return callback(false, doc);
      } else {
         return callback(true, null);
      }
   });
};

Then you can use your route /roomlist, to call the function getRoomlist when it gets hit and render the doc.

router.get('/roomlist', function(req, res){
  programLogic(function(err, doc){
    if(err){
      console.log(err);
    }
    return res.send(doc);
  });
});

If you still don't get it, just clone this and run through it

var programLogic = function (cb){
  MongoClient.connect(url, function(err, db) {
    assert.equal(null, err);
    getRoomlist(db, function(err, doc) {
      if(err){
        console.log(err);
      }
      return cb(false, doc);
    });
  });
}// programLogic ends

var getRoomlist = function(db, callback) {
   var cursor =db.collection('roomlist').find( );
   cursor.each(function(err, doc) {
      assert.equal(err, null);
      if (doc != null) {
         return callback(false, doc);
      } else {
         return callback(true, null);
      }
   });
};

router.get('/roomlist', function(req, res){
  programLogic(function(err, doc){
    if(err){
      console.log(err);
    }
    return res.send(doc);
  });
});

Upvotes: 1

grom
grom

Reputation: 76

Which engine do you use for views rendering? Based on error message, property "roomname" has been interpreted as a partial, and cannot be found in views directory.

Upvotes: 0

Related Questions