简体   繁体   中英

How to pass data form mongodb (using Mongous module) into nodejs view (using temp engine jade)?

Sorry to bother you folks. But i have a simple question. Ho can I pass data form model(database) into view. I use Express, Mongous(not Mongoose) to access MongoDB and Jade for my view-ing. I've tried Mongoose, but was not able to get even this far. The problem which I am having is how to actually pass data into view. Most examples that I've seen either have no mention of the views when database access is dicussed, or have manually created objects inside the views. I have not found any clear examples of data being fed into a view from the database. Please help, if you can...

This is what my model looks like:

        //Model Mongo DB
    var mgs = require('mongous').Mongous,
        dbColl = 'test.personnel';

    mgs(dbColl).find(function(data){
        return data;
    });

I require it in my app.js like so:

        require(__dirname + '/model');

It seems to work because I do get connection messages and I can see my data in the terminal. Here is DB info. My DB name is "test", collection name "personnel" and here what my data looks like:

        {
       "_id": ObjectId("4ef82a27b238f02ed9000000"),
       "cms": {
         "0": "Druapl_1"
      },
       "first_name": "Name_1",
       "last_name": "Lst_Name_1",
       "skills": {
         "0": "html_1",
         "1": "css_1",
         "2": "jQuery_1"
      }
    }

    {
       "_id": ObjectId("4ef81a0dcf163c7da3e5c964"),
       "cms": {
         "0": "Druapl_2"
      },
       "first_name": "Name_2",
       "last_name": "Lst_Name_2",
       "skills": {
         "0": "html_2",
         "1": "css_2",
         "2": "jQuery_2"
      }
    }

Please help, if you do it will exponentially improve my understanding of how data is presented, not just in nodejs, but in general.Thanks.

app.get('/', function(req, res) {
  mgs(dbColl).find(function(data){
    res.render('yourview.jade', { data: data });
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM