Hi,
Have an issue with a monitor I run every hour. Mostly it runs without a hitch but every so often I will get a TypeError: Cannot read properties of undefined (reading ‘authToken’)
Basically the monitor 1st runs a POST request which returns an auth token which I then extract to pass into the following requests.
Below are the post response tests I use to extract the token and test the response.
// Variable retrieval and response time threshold
pm.variables.get("variable_key");
var responseBody = pm.response.json();
// Extract the authToken from the response and save it to a variable
pm.globals.set("LMbearerToken", responseBody.data.authToken);
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("API response time is less than 2000ms", function () {
pm.expect(pm.response.responseTime).to.be.below(2000);
});
Any help appreciated.
Try wrapping your JSON parsing in a try catch, check if responseBody.data.authToken exists before using it, and log a clear message if it’s missing so the monitor doesn’t fail.
The error is telling you that it can’t find the authToken value on the data attribute within your response body.
Try logging your response body, to see what is being returned.
Is it timing out, or is there some other error?
You have tests for the status code and response time. What is the results of those tests when it fails?
You could add another test checking for the existence of the authToken.
Put the line where you set the variable within the test after the assertion, so it will only set the variable if the assertion passes.
You can nest test blocks, so you can add the test for the authToken within the status code test. There isn’t much point in testing for the authToken if the status code is incorrect.
Postman will stop processing code within a test block when it fails an assertion, which will include the nested test.
thanks, will try this and see how I go - appreciate the help
Its strange - when it fails like this the tests report No tests found.
The script should be in the Post-response tab.