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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:01:01+00:00 2026-06-09T21:01:01+00:00

I am writing a node js application. I am using request and cheerio to

  • 0

I am writing a node js application. I am using request and cheerio to load a set of URLs and get a bunch of information for the site, now let’s assume all I am trying to get is the title:

var urls = {"url_1", "url_2", "url_3",...,"url_n"};   
for(var i=0; i<urls.length; i++)
{
     getDOMTitle(urls[i],function(error,title){
         if(error)
            console.log("Error while getting title for " + urls[i]);
         else
            console.log("The title for " + urls[i] + " is " + title);
     });
}     

This is how my getDOMTitle method looks:

function getDOMTitle(urlReq,callback)
{
      var request = require('request');
      var cheerio = require('cheerio');
      request({url:urlReq},function(error, response, doc){
           var $ = cheerio.load(doc);
           if(error)
           {
              callback(true,null);
           }
           else
           {
              $('title', 'head').each(function (i, elem) {
                 var title = $(this).text();
                 callback(false,title);
               });     
           }
      }
}

In the case where the module throws an uncaught exception, how do I handle that situation?
I have tried adding the following:

  process.on('uncaughtException', function (err) {
    console.error(err);
    console.log("Node NOT Exiting...");
    callback(true,null);
  });

When I do that, I get an error saying I cannot set the headers once they have been sent. If I remove the callback from the process error handling, I do not see that error but the client spins for a long time because I assume we are never calling the callback.

How can I solve this?

Also, I have read somewhere that you can catch uncaught exceptions at the application level so you don’t have to replicate the code to catch it in every method, is that possible? and if it is and the method that threw the exception is expected to callback with some information, how can that be achieved?

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-09T21:01:03+00:00Added an answer on June 9, 2026 at 9:01 pm

    To answer your stated question, using an uncaught exception handler as a general error-trapping mechanism is commonly regarded as poor design. It’s a false economy to use it to handle anything other than non-recoverable situations where you just need to do some cleanup before exiting.

    You’ve got some problems in your example code. In your for loop, all the callbacks are going to report that they were working with the very last URL in your array because they’re all referring to the same copy of i, which will be at its highest value by the time any of them execute. You need to use a helper function or an immediate function invocation to give each callback a private copy of i.

    In getDOMTitle the error callback should be callback(error) and the code in your loop should include the returned value in the error message. The success callback should use null as its first parameter, though this is just a matter of convention.

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

Sidebar

Related Questions

I am working on re-writing an existing web site using Node.js with Express. Front-end
I'm writing a node application and I would like to get the name of
I'm currently writing a chat application which will run on Heroku. I'm using Node.JS
I'm writing a web application using node.js & express to provide a real-time aprs
After using node a lot, I had to get used to writing my code
A web app I am writing in JavaScript using node.js. I use Foreman, but
I'm writing an integration test for a rails application using Capybara within Rspec. After
So I am writing a mac application that parses SF Giants baseball statistics using
I am writing a java application. I have a class named Node. I have
I'm writing a Node.JS application that will store chat logs to a datastore (i.e.

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.