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

  • Home
  • SEARCH
  • 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 9183649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:51:27+00:00 2026-06-17T18:51:27+00:00

My js is as follows, /* * GET home page. */ var MongoClient =

  • 0

My js is as follows,

/*
 * GET home page.
 */

var MongoClient = require('mongodb'); 

exports.index = function(req, res){
    var studentObj = {};
    MongoClient.connect("mongodb://localhost:27017/MY_DB_TEST", function(err, db) {
      if(!err) {
        console.log("We are connected");        

        var collection =  db.collection('test');        
        var stream = collection.find().stream();

        stream.on("data", function(item){
            studentObj =  item;
        });     
      }
    });
    res.render('index', { title: 'Express', obj: studentObj });
};

I try to render this page with jade, my syntax for that is,

h1 Name: #{obj.name}

My JSON: (fetched from mongodb)

{
  name: "Ron",
  age: 24
}

This does not work, it only works if I keep my studentObj as a global variable i.e after the require statement.

Also, the obj.name is not rendered on first request, only for 2nd request onwards do I get the name property.

Why are these issues happening? am I missing something extremely trivial?

  • 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-17T18:51:28+00:00Added an answer on June 17, 2026 at 6:51 pm

    You’re trying to treat async code as synchronous. Don’t do that.

    Here’s your code:

    // Declare local var
    var studentObj = {};
    
    // fire off an async request
    MongoClient.connect("mongodb://localhost:27017/MY_DB_TEST", function(err, db) {
    
    // render, not waiting for data to arrive from server
    res.render('index', { title: 'Express', obj: studentObj });
    

    This also explains the behaviour with global var. At first render it’s still empty, but before second render the data request comes back and inits the global var. Solution is: render only after you got the data.

    stream.on("data", function(item){
      res.render('index', { title: 'Express', obj: item });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to get this following url using the downloadURL function as follows: http://www.ncbi.nlm.nih.gov/nuccore/27884304
Code is as follows but I get a ytplayer is not defined error. Is
Python's urllib2 follows 3xx redirects to get the final content. Is there a way
when using new Date,I get something like follows: Fri May 29 2009 22:39:02 GMT+0800
My Model Class as follows: public class Employee { public Guid ID { get;
I am running PHP unit as follows: phpunit --debug --verbose --coverage-clover=/tmp/test.xml and get the
I have a menu: <div class=headerMenu> <ul> <li><a href=#>Home <span>Home Page<span></a></li> <li><a href=#>About <span>About
I have a home page in which i have two DIVs (left & right).
I run the following code on a page (#home) that should smoothly inject a
the php page is not receiving the GET variable and is producing an undefined

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.