简体   繁体   中英

Javascript runtime error: Cannot read property content from undefined

I have some tricky test case with JScript. I have nested for-loop in Javascript. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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