0
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.

5
  • how is it not working? What is it doing? What are you expecting it to do? What errors is it raising? Commented Dec 7, 2015 at 13:04
  • Failed to lookup view "roomname" in views directory Commented Dec 7, 2015 at 13:09
  • I added 2 braces {} before that function code and it throws the latest error .. Commented Dec 7, 2015 at 13:10
  • h1 RoomList select each room, i in roomlist option =room.roomname <---This is my view . Commented Dec 7, 2015 at 13:12
  • If any answer has provided solution to your problem, you should accept it and close the question. If you've found the solution yourself, you should post it here and accept it as solution thereby closing the question. Commented Dec 15, 2015 at 11:48

2 Answers 2

1

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);
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

router.get('/userlist', function(req, res) { var db = req.db; var collection = db.get('usercollection'); collection.find({},{},function(e,docs){ res.render('userlist', { "userlist" : docs }); }); }); <--- this one is also using the same database. Only tables vary. this one is working properly . why not the other thing???
0

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.

5 Comments

I am using express templating
@sriramraghunathan have you set everything correctly? are you use jade, swig or maybe handlebars? expressjs.com/en/guide/using-template-engines.html
yes everything is perfect .I am sure because i am using the same to get the userlist which is working perfectly.
The only difference between working example (users), and not working example (rooms), is: for room you pass only callback as a parameter for find method; for users you pass empty query {}, empty options {}, and then, as last argument the callback.
I did that and it is not working. any other suggestion @grom??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.