2

Basically, I want to show particular fields from "employees" collection into a html page. But even after searching a lot on web, I'm unable to do so.

Here is the route part from the server.js file:

app.get('/fetching', function(req, res){
    connection.fetcher(function(data)
        {
            res.render("testing.html",data);
        }
    );
});

Now this is the part from connection.js file:

var fetcher= function(callback) {
    var mongodb = require('mongodb');
    var MongoClient = mongodb.MongoClient;

    var url = 'mongodb://localhost:27017/HippoFeedo';

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

            console.log('Unable to connect to the mongoDB server. Error:', err);
        }

        else {
            console.log('Connection established to', url);

            // Get the documents collection
            var collection = db.collection('employees');

            collection.find({},function (err, result) {
                if (err) {
                    console.log(err);
                } else {

                    console.log(result);
                    callback(result);
                }

            });
        }
    });

Now, findOne is working fine and returning the value to server.js file perfectly. But I need to use "find", so how to send the complete array to the server.js through callback?

And moreover, I need to send that retrieved data from server.js to a HTML file called testing.html through rendering and display it through angular js. Please explain a simple way to do so.

EDIT: I got to know how to work with "find", I just used "toArray" alongwith "find" in function. And now, I'm able to return the value to server.js through call back. But the other question is still unsolved: How do I pass those values to the html page?

2
  • you can look through jade for populating data in the UI side Commented Dec 8, 2015 at 15:09
  • I dont want to use jade or ejs.. Is that possible to do that using angularjs or jquery? Commented Dec 9, 2015 at 5:10

1 Answer 1

3

Using ejs, you need to set the view engine:

app.set('view engine', 'ejs');

Then get your data:

 app.get('/employees',(req , res) =>{
    db.collection('employees').find().toArray(function(err , i){
        if (err) return console.log(err)

        res.render('index.ejs',{employees: i})  
     })
 });

The .ejs file would be like this:

employees
  <ul class="employees">
  <% for(var i=0; i<employees.length; i++) {%>
    <li class="employees">
      <span><%= " Nome: " +employees[i].name+"."%></span>
      <span><%=" Address: " + employees[i].address%></span>
    </li>
  <% } %>
</ul>

Just a simple way using ejs. Hope it helps to clarify things.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.