简体   繁体   English

Javascript 运行时错误:无法从未定义中读取属性内容

[英]Javascript runtime error: Cannot read property content from undefined

I have some tricky test case with JScript.我有一些棘手的 JScript 测试用例。 I have nested for-loop in Javascript.我在 Javascript 中嵌套了 for 循环。 In the outer loop and the inner loop also I have the below statements (with different variable, but statements are similar) to execute the client requests.在外循环和内循环中,我也有以下语句(具有不同的变量,但语句相似)来执行客户端请求。

var targetURL = "http://firsturl.com";
var targetURL_Req = new Request(targetURL, "GET", authHeaders);
var targetClientReq = httpClient.send(targetURL_Req);
targetClientReq.waitForComplete();
targetClientRes = JSON.parse(targetClientReq.getResponse().content);

When the loop is running, for some values in the loop, I am getting successful response.当循环运行时,对于循环中的某些值,我得到了成功的响应。 But for some, it is giving the response as below.但对于某些人来说,它给出的响应如下。 The tricky part is, if I execute multiple times, then the failure is coming at random places.棘手的部分是,如果我多次执行,那么失败就会随机出现。

Execution of ParseJSONResponse failed with error: Javascript runtime error: \"TypeError: Cannot read property \"content\" from undefined. (ParseJSONResponse.js:7)

Initially I thought like one particular value is giving this error.最初我认为一个特定的值是给出这个错误。 But, the error is coming randomely.但是,错误是随机出现的。 What ever value it was failed previously, getting success in the next execution and vice versa.之前的任何价值都失败了,在下一次执行中获得成功,反之亦然。 But the error always coming is standard as mentioned above.但是如上所述,总是出现错误是标准的。

Am I missing any major thing here?我在这里错过了什么重要的事情吗?

It appears some of the HTTP Request didn't return value caused the error.似乎某些 HTTP 请求没有返回值导致错误。 You can try add the code snippet below:您可以尝试添加下面的代码片段:

 // Get and Process the response if (targetClientReq.isSuccess()) { var responseObj = targetClientReq.getResponse().content; return responseObj.access_token; } else if (targetClientReq.isError()) { throw new Error(exchange.getError()); }

See more: https://docs.apigee.com/api-platform/antipatterns/wait-for-complete查看更多: https://docs.apigee.com/api-platform/antipatterns/wait-for-complete

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM