邮递员将响应数组的最高ID设置为环境变量
我的跑步者中有一个端点X,该端点X返回包含大量对象的数组(每个都带有“ ID”参数)。作为最后一个创建的对象始终具有最高ID。
在跑步者的下一步中,我需要从具有最高ID的对象使用一些参数(“ ID”和“ taskLinkKey”),由Endpoint X返回。 因此,我使用此公式将它们设置为环境变量:
const responseJson = pm.response.json();
let arr = responseJson;
let lastTask = arr.length -1;
pm.environment.set("taskId", arr[lastTask].id);
pm.environment.set("taskLinkKey", arr[lastTask].taskLinkKey);
console.log(pm.variables.get("taskId"));
console.log(pm.variables.get("taskLinkKey"));
如您所见,端点X返回的数组中的最后一个对象的参数设置为我的环境变量。但是问题是,有时在调用端点x时,返回数组中的最后一个对象不是具有最高ID的对象。我不知道为什么,我对此无力。 您能否提出一种解决方案,该解决方案将将对象的“ ID”和“ TaskLinkKey”参数设置为最高ID作为我的环境变量(而不是数组中的最后一个对象)?
例如: 端点x返回:
[
{
"encodedKey": "8a19c3cb80fa96bb0180fad7c00f6170",
"id": 2761,
"creationDate": "2022-05-25T10:53:59+0000",
"lastModifiedDate": "2022-05-25T10:53:59+0000",
"dueDate": "2022-05-26T00:00:00+0000",
"taskLinkKey": "8a19a20c80f8e1f70180fada15401a7e"
},
{
"encodedKey": "8a19c46680f8e7b80180fa8af13c563c",
"id": 2711,
"creationDate": "2022-05-25T09:27:31+0000",
"lastModifiedDate": "2022-05-25T09:27:32+0000",
"dueDate": "2022-05-26T00:00:00+0000",
"taskLinkKey": "8a19a20c80f8e1f70180fa70f1f20337"
},
{
"encodedKey": "8a19d13180f8d7e20180fae137b431f5",
"id": 2771,
"creationDate": "2022-05-25T11:01:47+0000",
"lastModifiedDate": "2022-05-25T11:01:47+0000",
"dueDate": "2022-05-26T00:00:00+0000",
"taskLinkKey": "8a19a24680f8cdfb0180fadbb8524fc3"
},
{
"encodedKey": "8a19c46680f8e7b80180fa8af13c563e",
"id": 2712,
"creationDate": "2022-05-25T09:27:37+0000",
"lastModifiedDate": "2022-05-25T09:27:37+0000",
"dueDate": "2022-05-26T00:00:00+0000",
"taskLinkKey": "8a19be1e80f21ed60180fa8b02c9678f"
}
]
我想在之后设置两个环境变量:
ID:2771
TaskLinkKey:8A19A24680F8CDFB0180FADBBBBBB8524FC3
I have an endpoint X in my runner which returns the array containing tons of objects (each with "id" parameter). The object created as the last one has always the highest id.
In the next step of the runner I need to use some of the parameters ("id" and "taskLinkKey") from the object with the highest id, returned by endpoint X.
So I'm using this formula to set them as environmental variables:
const responseJson = pm.response.json();
let arr = responseJson;
let lastTask = arr.length -1;
pm.environment.set("taskId", arr[lastTask].id);
pm.environment.set("taskLinkKey", arr[lastTask].taskLinkKey);
console.log(pm.variables.get("taskId"));
console.log(pm.variables.get("taskLinkKey"));
As you can see, the parameters from the last object in the array returned by endpoint X are set as my environmental variables. But the problem is that sometimes when endpoint X is called, the last object in the returned array is not the one with the highest id. I don't know why and I have no power over it.
Could you suggest a solution that will set "id" and "taskLinkKey" parameters of the object with highest id as my environmental variables (instead of the last object in the array)?
For instance:
Endpoint X returns:
[
{
"encodedKey": "8a19c3cb80fa96bb0180fad7c00f6170",
"id": 2761,
"creationDate": "2022-05-25T10:53:59+0000",
"lastModifiedDate": "2022-05-25T10:53:59+0000",
"dueDate": "2022-05-26T00:00:00+0000",
"taskLinkKey": "8a19a20c80f8e1f70180fada15401a7e"
},
{
"encodedKey": "8a19c46680f8e7b80180fa8af13c563c",
"id": 2711,
"creationDate": "2022-05-25T09:27:31+0000",
"lastModifiedDate": "2022-05-25T09:27:32+0000",
"dueDate": "2022-05-26T00:00:00+0000",
"taskLinkKey": "8a19a20c80f8e1f70180fa70f1f20337"
},
{
"encodedKey": "8a19d13180f8d7e20180fae137b431f5",
"id": 2771,
"creationDate": "2022-05-25T11:01:47+0000",
"lastModifiedDate": "2022-05-25T11:01:47+0000",
"dueDate": "2022-05-26T00:00:00+0000",
"taskLinkKey": "8a19a24680f8cdfb0180fadbb8524fc3"
},
{
"encodedKey": "8a19c46680f8e7b80180fa8af13c563e",
"id": 2712,
"creationDate": "2022-05-25T09:27:37+0000",
"lastModifiedDate": "2022-05-25T09:27:37+0000",
"dueDate": "2022-05-26T00:00:00+0000",
"taskLinkKey": "8a19be1e80f21ed60180fa8b02c9678f"
}
]
And I want to have two environmental variables being set afterwards:
id: 2771
taskLinkKey: 8a19a24680f8cdfb0180fadbb8524fc3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要迭代响应中的所有元素。
第一次,您设置环境变量(如果响应只有一个项目,或者第一项具有最高的ID)。
对于以下每个项目,您可以检查ID是否比已经存储的内容大。如果是,则覆盖新值。
You need to iterate over all the elements in the response.
First time, you set the environment variables (in case the response has only one item, or the first item has the highest ID).
For every following item, you check if the ID is bigger than what you have stored already. If yes, you overwrite with the new value.