NODE JS cancel request

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am using this code to download files in node js :

var currentVideoRequest = null;

window.spawnVideoPlayer = function (url, subs, movieModel,tag) {
    if(currentVideoRequest) {
    try
    {
      currentVideoRequest.abort();
    }
    catch(err)
    {
      alert(err.message);
    }

    }

    var fs = require('fs');
    var urlrequest = currentVideoRequest = require('request');

    urlrequest.get({url: url, encoding: 'binary'}, function (err, response, body) {
      fs.writeFile(FILEURL, body, 'binary', function(err) {

      }); 
    });
}

And in the currentVideoRequest.abort(); i get this error:

    Object function request(uri, options, callback) {
  if (typeof uri === 'undefined') throw new Error('undefined is not a valid uri or options object.')
  if ((typeof options === 'function') && !callback) callback = options
  if (options && typeof options === 'object') {
    options.uri = uri
  } else if (typeof uri === 'string') {
    options = {uri:uri}
  } else {
    options = uri
  }

  options = copy(options)

  if (callback) options.callback = callback
  var r = new Request(options)
  return r
} h
profilepic.png
manpreet 2 years ago

To add to @Etai's answer, you need to require the request module before using it for one instance of the request. Something like this:

var request = require('request');
// ...
// then later in the code
var urlrequest = request.get(uri, function(err, response, body) {
    // process data here
});

// later, you'd abort this as:
urlrequest.abort();

Note that I'm saving the instance with var urlrequest = request.get(params, callback); so that I can call abort on it later.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.