How to check if no data returned when setting enviroment varaibles

I am testing an API that I use a date variable of approx 7 days ago to get data, every now and then the test fails as the environment variable I try set, “id” is undefined. This is because no data has been entered in the last 7 days. When no data is returned it is an empty array.

How should I check if there is data returned before trying to set environment variables

Current structure is

Get the response:
var responseObject = pm.response.json();
Set the env variable:
pm.environment.set(“activityId”, responseObject[0].Id);

Have tried a few different If statements but not really sure whether I should be testing the whole response as empty or something as undefined.

Thanks in advance.

So this works, but wondering if there is a better or easier way?


console.log("Body: " + pm.response.json());
    console.log("Body is empty");    
    console.log("Body not empty");
    pm.environment.set("activityId", responseObject[0].Id);