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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:13:00+00:00 2026-05-27T00:13:00+00:00

I’m using a route to process a url. The functions fire as expected. What

  • 0

I’m using a route to process a url. The functions fire as expected. What I’m trying to do is use the response.write() inside of a call back. I understand this isn’t working because the call back doesn’t have access to the same variables as the function that calls it, but I’m wondering what the correct NODE way of doing this would be.

route.post('/{type}subject/{method}', function (request,response) {
var post = "";
request.on('data', function (chunk){
    post += chunk;
});
request.on('end', function (){
    postData = qs.parse(post);
    response.writeHead(200);
    switch(request.params['method'].toLowerCase())
    {
        case "registerobserver":
            if (postData['uri']){
                registerObserver (request.params['type'], postData['uri']);
                response.write(success("registerobserver"));
            }
            else
                response.write(failure("1","uri undefined"));

            break;
        case "unregisterobserver":
            if (postData['uri']){
                client.query ('DELETE observers FROM observers INNER JOIN type ON (observers.TypeID = type.TypeID) WHERE observers.uri ="'+postData['uri']+'" AND type.name = "'+request.params['type']+'"', 
                function(err, info) 
                {
                    if(err){
                        response.write(failure("2", "general database failure"));}
                    else{   
                    if(info.affectedRows != 0)
                        response.write(success("unregisterobserver")); //this code does not trigger a response due to namespace

                    else
                        response.write(failure("1", "uri not an observer"));//this code does not trigger a response
                        console.log("uri not observer");
                    }

                    console.log("done");
                })

            }
            else
                response.write(failure("1","uri required"));

            break;
        default:

    }

    response.end();
})
//response.write("type: "+request.params['type']+"<br/>method="+request.params['method']);

});

function success(method){return "<?xml version=\"1.0\"?>\n<response stat=\"ok\">\n\t<method>"+method+"</method>\n</response>";}
function failure(code, message){return "<?xml version=\"1.0\"?>\n<response stat=\"fail\">\n\t<err code=\""+code+"\" msg = \""+message+"\" />\n</response>";}
  • 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-05-27T00:13:01+00:00Added an answer on May 27, 2026 at 12:13 am

    Basically what happens is that the async query handler function will be called after your response.end() function call. As such any writes will fail.

    You need to call response.end() from inside the callback instead, and take care to not otherwise call response.end() once you are in async code path. Ie. return immediately after the client.query() call.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from

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.