Http statusCode is sometimes undefined
I am using request module in my node.js (express) application. Sometimes this error occurs with a status code:
TypeError: Cannot read property
'statusCode' of undefined at Request._callback
This is all my 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
sometimes causes an error. How can I control that the request is successful? Is this a bug in the request module? Why is statusCode undefined sometimes? I think the statusCode should be available every time.
Answer This is probably a response timeout issue and you should be doing an if statement like this:
if (response === undefined || response.statusCode != 200){ console.log("there is a prob"); }
this code first controls the response variable and then the response.status code, so if the response is undefined does not control response.statusCode, we cannot get any errors.
+3
source to share