I am trying to use the following snippet to filter an API response body while utilizing a variable as the search value (ObjectID):
var data = JSON.parse(responseBody);
postman.setEnvironmentVariable(“ReportID”, data.filter(function(column) {
return column.ObjectID===(pm.environment.get(“ObjectID”));
})[0].ReportID);
I have gotten it to work once, and now it returns an error of “There was an error in evaluating the test script: TypeError: Cannot read property ‘ReportID’ of undefined.”
I am able to get a similar request to work when I utilize a hard coded search value (Administrators):
var data = JSON.parse(responseBody);
pm.environment.set(“RoleID”, data.filter(function(column) {
return column.Name===“Administrators”;
})[0].ID);
Is there an easier way to set a variable from a filter function of an API response?