Sandbox Script Errors

Hi all,
Glad to finally be here. We have the following API problem(ofc beautified) :smiley:
Post asset creates an asset, let us assume the following:

“id”: “51fc4807-d3ed-4b6e-b1f8-ee3eb2e18a96”,
“name”: “Tolik87”,
“description”: “APIDescription”,
“status”: “DAMNED”,

and after that another POST that changes the status of the asset to NOT-DAMNED (it is a job that takes around 3 mins). I want to be able to poll it for when the status becomes NOT-DAMNED, so hitting the API every 30 sec to check if the status is NOT-DAMNED
This should do the trick:
const response = pm.response.json();

if (response.status !== “NOT-DAMNED”) {
setTimeout(() => {}, 30000)
postman.setNextRequest(pm.info.requestId)
}

However I am looking for something to break my loop if it never becomes “NOT-DAMNED”. Anyone can help with that. Tried to save counter in an environment variable but maybe am too stupid to write that script by myself :frowning:

setTimeout(() => {}, 30000)
const response = pm.response.json()
if (!pm.environment.has(“counter”)) {
console.log(“set counter to 0”);
pm.environment.set(“counter”, 0);
}

let currentCount = parseInt(pm.environment.get(“counter”));
console.log(currentCount)

if (currentCount < 10) {
if (response.status === “NOT-DAMNED”) {
postman.setNextRequest(“Next Request”);
}
currentCount++;
pm.environment.set(“counter”, currentCount);
postman.setNextRequest(pm.info.requestId);
}

I came till here but that does not work properly. Added setTimeOut and now when I log count, it stays the same :frowning:

Hi @tolik87

If I’m not mistaken, “status” is not a “response” property but a “pm.response” property.

I’ve followed an Udemy course about postman where the instructor always use the same const response = pm.response.json();.
I think this is a very bad practice because it is not a response but the data of a reponse.
I recommand you to change to “respData” or “jsonData”…
(“data” seems to be used by postman when you use a csv file, and represents a line, so, I don’t use it)

Hope this helps…

Best

Didier

EDIT: this post was in the top of the list, I did not see it was one year old… maybe it could help someone else :slight_smile:

@ddegey

response.status is perfectly valid consider the original posters example response.

Status is a key in that response.

Not to be confused with the status on a assertion which is a function from Postman to help assert status codes.

pm.expect(response).to.have.status(200)

Two completely separate things.

To answer the original posters question.

Indefinite loops are a bad thing, so you would check for the NON-DAMNED status and a configurable counter object with the counter being the trigger that prevents the indefinite loop.

I can create an example, but as this is over a year old, I’m only going to do that if someone wants it (or I’ll just point to relevant questions on Stack Overflow as there are a bunch of questions related to best practice for loops on there).

@michaelderekjones
Oh yes, bien vu…
I’ve missed that…
Pfff how I’ve associated status and response without thinking… :sweat_smile:

Thanks

Didier