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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:49:57+00:00 2026-06-12T17:49:57+00:00

My college course just started going over mongodb and we have an assignment to

  • 0

My college course just started going over mongodb and we have an assignment to work on. Everything seems to be going well so far, but I have ran into an issue returning a value in my response.write. Scope issues seem to always be the ones that plague me. Here is the code:

var querystring = require("querystring"),
fs = require("fs"),
mongodb = require('mongodb'),
server = new mongodb.Server('localhost',27017, {auto_reconnect: true}),
db = new mongodb.Db('testdb',server);

db.createCollection('testCollection', {safe : true}, function(err, collection){});



function createData(response, postData) {
    var fname = querystring.parse(postData).fname;
    var lname = querystring.parse(postData).lname;
    var phone = querystring.parse(postData).phone;

    var info = {'fname':fname,'lname':lname,'phone':phone};
    var infojson = JSON.stringify(info);
    var infojson = JSON.parse(infojson);

    db.open(function(err, db) {
            if(!err) {
                    db.collection('testCollection', function(err, collection) {
                            collection.insert(infojson, {safe : true}, function(err, result) {
                                    if(err) {
                                            console.log(err);
                                    } else {
                                            console.log(result);
                                            db.close();
                                    }
                            });
                    });
            }
    return result;
    });
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("You have added " + result + " to the database.");
response.end();
}

Here is the error I am getting:

/home/ubuntu/151_final/requestHandlers.js:36
response.write("You have added " + result + " to the database.");
                                   ^
ReferenceError: result is not defined
at Object.createData [as /createData] (/home/ubuntu/151_final/requestHandlers.js:36:40)
at route (/home/ubuntu/151_final/router.js:4:19)
at IncomingMessage.<anonymous> (/home/ubuntu/151_final/server.js:18:4)
at IncomingMessage.EventEmitter.emit (events.js:85:17)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at Socket.socket.ondata (http.js:1690:22)
at TCP.onread (net.js:402:27)
  • 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-12T17:49:59+00:00Added an answer on June 12, 2026 at 5:49 pm

    I think you should do

      db.open(function(err, db) {
                if(!err) {
                        db.collection('testCollection', function(err, collection) {
                                collection.insert(infojson, {safe : true}, function(err, result) {
                                        if(err) {
                                                console.log(err);
                                        } else {
                                                console.log(result);
                                                db.close();
                                                response.writeHead(200, {"Content-Type": "text/plain"});
                                                response.write("You have added " + result + " to the database.");
                                                response.end();
                                        }
                                });
                        });
                }
    
        });
    

    This is not a scope issue it’s just that the call is async and result it’s not defined because the line

    response.write("You have added " + result + " to the database.");
    

    is executed immediately, it doesn’t wait for the other function to return

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

Sidebar

Related Questions

I just started working with node.js and json objects in my college course. One
I have to create a small android application for my college course work which
I have just taken java programming course in college. And we were asked to
I just started taking an Assembly Language course in College. We are using the
I have been trying to complete the following assignment for my college. So far,
I am working on an assignment for a college course involving json and node.js.
I've studied C++ as a college course. And I have been working on it
My college is going to start soon, but I want to do something in
I am taking an HTML / Web Design course and just started it the
I just finished a college course in C. The book used was Kochan's. I

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.