我在node.js(express)应用程序中使用请求模块。有时会发生与statusCode相关的错误:
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这是我的全部代码:
request("www.googleapis/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 有时会出错。如何控制请求成功?它是请求模块中的错误吗?为什么statusCode有时会被定义?我认为statusCode应该每次都可用。
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.
答案 可能是响应超时问题,你应该做一个if语句这个;
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"); }这段代码首先控制响应变量然后是response.statuscode,所以如果响应未定义则不控制响应.statusCode因此,我们不能得到任何错误。
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.
推荐答案这是一种解决方法,因为可能有几个原因回复是未定义:
This is a workaround, as there might be several reasons why response is undefined:
if (!error && response.statusCode == 200) { // do your stuff here.. }更多推荐
http statusCode有时未定义
发布评论