6

I'm using request module in my node.js(express) application. Sometimes this statusCode related error occurs:

TypeError: Cannot read property
'statusCode' of undefined at Request._callback

This is my whole code:

request("https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + docs[0].title + "&type=video&key=(some-key-variable)", {
                json: true
              }, function(error, response, resultData) {
                var yArr = [];
                if (!error || response.statusCode == 200) {
                  for (var i = 0; i < config.youtubeVideoCount; i++) {
                    var vArr = resultData.items[i];
                    yArr.push(vArr);
                  }
                } else {
                  console.log("can't find video");
                }
              });

response.statusCode gives an error sometimes. How can I control that the request is successful? Is it bug in the request module? Why is statusCode undefined sometimes? I think statusCode should be available every time.

Answer Probably it is response timeout issue and u should do an if statement like this;

if (response === undefined || response.statusCode != 200){ console.log("there is a prob"); }

this code firstly control response variable and then response.statuscode , so if response undefined don't control response.statusCode thus , we can't get any error.

Erdi
  • 1,824
  • 3
  • 21
  • 31
  • 1
    If the error is truthy then `response` my be undefined but it will still check `response.statusCode` – Explosion Pills Apr 20 '15 at 15:41
  • When statusCode is undefined, what is the value of readyState? I have seen browser environments that (incorrectly) don't set a status value until readyState is 2 or greater. – dgvid Apr 20 '15 at 15:42
  • there is no readyState property into response , console log was undefined everyime – Erdi Apr 20 '15 at 15:53

1 Answers1

7

This is a workaround, as there might be several reasons why response is undefined:

if (!error && response.statusCode == 200) {
    // do your stuff here..
}
Nikos M.
  • 8,033
  • 4
  • 36
  • 43
  • What reasons can be ? – Erdi Apr 23 '15 at 22:52
  • @OsmanErdi, there can be many, e.g timeout issues, bugs in implementation and a bunch of others. In any case you should use a workaround to be clear – Nikos M. Apr 24 '15 at 10:55
  • i discovered that error parameter in request module controls response undefined or null , if null or undefined , return error because of this , we should write error control at top pf if statement ex; if (error || response.statusCode != 200) it is true – Erdi Apr 29 '15 at 18:37
  • @OsmanErdi,, edited, correct the error argument describes and signifies any errors in the request, howevr i think this bis what you need `if (!error && response.statusCode == 200)`, since you need to run your code when no error exists and the request is indeed found (200 statusCode) – Nikos M. Apr 30 '15 at 05:08
  • How could if (!error && response.statusCode == 200) be a workaround when response can be undefined? – pete May 17 '19 at 03:30