http statusCode 有时未定义

编程入门 行业动态 更新时间:2024-10-26 07:39:09
本文介绍了http statusCode 有时未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在我的 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 应该每次都可用.

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 变量,然后是 response.statuscode ,所以如果 response undefined 不控制 response.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.

推荐答案

这是一种解决方法,因为 response 为 undefined 的原因可能有多种:

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 有时未定义

本文发布于:2023-11-26 15:28:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634272.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未定义   http   statusCode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!