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 7739411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:30:58+00:00 2026-06-01T08:30:58+00:00

I am new to nodejs but I did get something to work last night

  • 0

I am new to nodejs but I did get something to work last night with mongodb on a IIS server with iisnode. 🙂

But I am wondering over one thing which seems to be a refresh bug or something.
When I go to “http://localhost/mongo.js” in my browser the results will just be “[]” the first time. If I hit refresh the results will be what I expected (an json array with persons). Dosen’t this seems wrong?

Let’s say now I do it with a query, “http://localhost/mongo.js?name=Daniel”, and get all Persons with the name Daniel. The response the first time will be “all the persons” because that’s what we asked for above, and when I hit refresh the results will be all the Persons named Daniel. Why is this happening?
It seems that the server cached the query’s i’ve made, and I don’t want to hit refresh everytime to get the correct results.

This is my code I am using: (also available here http://pastebin.com/PnVfrQmh)

/* GLOBALS
----------------------------------------------------------------------*/
var rdata = [];

/* SERVER SETTINGS
----------------------------------------------------------------------*/

//load http module to ceate an http server. 
var http = require('http');
var url  = require('url');

//configure to respond http server with message
http.createServer(function (request, response) {

    //request name parameter
    var url_parts = url.parse(request.url, true);
    var query = url_parts.query;

    //do the mongo
    var mongo = require('mongodb');
    var db = new mongo.Db('nodedb', new mongo.Server('localhost', 27017, {}), {});

    db.open(function() {

        db.collection('Persons', function(err, collection) {

            var cursor = collection.find(query);

            cursor.each(function(err, doc) {

                if(doc) {

                    rdata.push(doc);

                }

            });

        });

    });

    //write what type of response
    response.writeHead(200, {'Content-Type': 'application/json;charset=utf-8'});

    //return data json array
    response.end(JSON.stringify(rdata));

    //clear rdata
    rdata = [];

}).listen(process.env.PORT); 
  • 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-01T08:30:59+00:00Added an answer on June 1, 2026 at 8:30 am

    You have to remember that you are always in an async world with node.js. This caught me out as well coming from a more sync background.

    What is happening here is your response is returning before the logic is run. This is because when you call db.open(function() { this goes on the event loop and returns. This is non-blocking so the next line of code that runs is response.writeHead(200, {'Content-Type': 'application/json;charset=utf-8'});. Then the callback for db.open is called, sometime in the future.

    In order to correct this. return the response after the cursor.each(function(err, doc) { loop is finished.

    Hope this helps.

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

Sidebar

Related Questions

I asked a similar question last week but did not get an answer that
New to NodeJS. Yes I know I could use a framework, but I want
I am new to nodejs. I managed to install node.js on amazon EC2 server
I'm pretty new to Node.js/Express, but I think I'm slowly getting the hang of
So I'm new to OAuth and Node.JS, but I have to create a Flickr
I'm using Nodejs and Socket.io. When the client connects, new JavaScript objects are created.
Here is the application on codeplex all i did is created a new text
I'm new to nodejs, the only way that I can come up with is
Did anyone set up something like this for himself using the existing node.js REPL?
I'm using Nodejs to insert some data via MongoDB as so: var db =

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.