Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8154311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:18:11+00:00 2026-06-06T16:18:11+00:00

I wanted to extract data from mongodb and pass it to the view. Everything

  • 0

I wanted to extract data from mongodb and pass it to the view. Everything seems to work, but instead of seeing all 10000 records show up I see only one. I feel that I am very close to solving it, but I got stuck… I am using node-mongodb-native, express and dot.js to achieve my goal.

Here is my app.js, look for the app.get() that’s where all the action is:

    /**
 * Mongo DB
 */
var mongodb = require('mongodb'),
    serverdb = new mongodb.Server('127.0.0.1', 27017, {}),
    dbName = new mongodb.Db('test', serverdb, {});
/**
 * Module dependencies.
 */
var express = require('express'),
    doT = require('doT'),
    app = express.createServer(),
    pub = __dirname + '/public',
    view =  __dirname + '/views';

// Configuration
app.configure(function(){
    app.set('views', view);
    app.set('view options', {layout: false});
    app.set('view engine', 'html');
    app.use(app.router);
});

//Simple templating
app.register('.html',doT, {
    compile: function(str, opts){
        return function(locals){
            return str;
        }
    }
});


//----------------- This is where the problem is i think ---------------
app.get('/', function(req, res){
    dbName.open(function (error, client) {
        var collection = new mongodb.Collection(client, 'personnel');
        collection.find().each(function(err, data){
            //Error check
             if (err){return res.end('error!'+err);}
            //Data
             if (data){
                res.render('index.html',{data:data._id});
             } else {
                 res.end();
             }
        });
    });
});
//--------------------------------------------------------------


app.configure('production', function(){
    app.use(express.errorHandler());
});
app.listen(3000);
console.log('Express server listening on port %d in %s mode', app.address().port, app.settings.env);

And here is my html:

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Index</title>
</head>
<body>
    {{=it.data}}
</body>
</html>

Please help if you can. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:18:14+00:00Added an answer on June 6, 2026 at 4:18 pm

    Well, I finally found a solution, first I had to change from each() to toArray() where retrieves my data is retrieved. then I stringify-ed my data. It’s not an ideal solution, since I just print out everything as one string as opposed to iterating through the data in the template. I am just glad I am moving in the right direction.

    My app.js:

    EDIT: found even better solution.

        //Replace this code with the one in my question to get full file
        //----------------- This is where the problem was ---------------
        app.get('/', function (req, res) {
            dbName.open(function (error, client) {
                var collection = new mongodb.Collection(client, 'some_collection');
                collection.find().limit(300).toArray(function (err, dataObjArr) {
                    var data = '';
                    var dataArr = [];
                    var i = dataObjArr.length;
                    //check for error
                    if(err){return res.end('error!'+err);}
                    //Data
                    if (dataObjArr) {
                        while(i--){
                            dataArr[i] = dataObjArr[i]._id;
                        }
                        data = dataArr.join(' ');
                        res.render('index.html', { returnedData : data });
                    }else{
                        res.end();
                    }
                });
            });
        });
        //--------------------------------------------------------------
    

    my index.html:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset='utf-8'>
    <title>Index</title>
    </head>
    <body>
        {{=it.returnedData}}
    </body>
    </html>
    

    Like I said, it’s not ideal solution, but I think now I have to put advanced features of the templating engine itself to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to extract some data from an SVG file. I know that SVG
I wanted to extract the Signer Informations from PKCS#7 Signed Code Image using C/CPP.
I have developed a GUI that is used to extract some data from a
How do I extract the data from that XML object, which is a value
UPDATE I was able to get everything to work properly and I just wanted
I am trying to extract data from website using Perl API. I am using
I am trying to extract data from a table using... $tags = $xpath->query('//tr[@valign=top]/td/span[@style=background-color:#cccccc;font-weight:bold;]'); on
I'm trying to extract a set of data from some (large) text files. Basically,
Lets say I wanted to scrape a webpage, and extract some data. I'd most
I'm trying to extract data from a MySQL server with PHP. The information has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.