邮递员从数组中获取值并在env变量上进行动态设置 [英] postman get values from array and set it,dynamically ,on env variables

查看:103
本文介绍了邮递员从数组中获取值并在env变量上进行动态设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[
    {
        "id": "2448ec2e-b849-4835-9c6a-02ea7b28f298",
        "name": "test sequence for Abort",
        "description": "This a test sequence",
        "type": "Product",
        "createDate": "2018-08-09T21:01:47.3120000Z",
        "lastUpdateDate": "2018-08-09T21:01:47.3120000Z",
        "lastUpdateUser": "José Carlos",
        "variables": [
            {
                "id": "afa2f2e1-e3be-4dff-a7ad-817b7d25be64",
                "name": "Result1",
                "type": "string"
            },
            {
                "id": "a0914b11-43ff-475a-ac44-65da1fd68aea",
                "name": "Result2",
                "type": "string"
            },
            {
                "id": "5a3277d3-386d-4a12-b373-af38d91a727d",
                "name": "Result3",
                "type": "string"
            },
            {
                "id": "61fc107a-8929-4a33-8f26-2a0228344adc",
                "name": "Result4",
                "type": "string"
            },
            {
                "id": "598a1843-12f0-42ea-819d-944c1e1d6060",
                "name": "Result5",
                "type": "string"
            },
            {
                "id": "c6182b5f-df2f-4d6d-91b0-77e8679f052f",
                "name": "Result6",
                "type": "string"
            },
            {
                "id": "a368b7db-3d0c-4385-9f56-ad9793072b97",
                "name": "Result7",
                "type": "string"
            },
            {
                "id": "ae5db407-ce44-485c-a1a2-e0ae33353279",
                "name": "Result8",
                "type": "string"
            },
            {
                "id": "760bb1f1-70f6-49e9-a9b4-9e8d7e26b14a",
                "name": "Result9",
                "type": "string"
            }
        ],
        "instructions": null
    },
    {
        "id": "888108a7-2101-4aeb-b1ec-6ad779b8c09d",
        "name": "test sequence for initial",
        "description": "This a test sequence",
        "type": "0",
        "createDate": "2018-08-09T21:02:24.8290000Z",
        "lastUpdateDate": "2018-08-09T21:02:24.8290000Z",
        "lastUpdateUser": "José Carlos",
        "variables": [
            {
                "id": "afa2f2e1-e3be-4dff-a7ad-817b7d25be64",
                "name": "Result1",
                "type": "string"
            },
            {
                "id": "a0914b11-43ff-475a-ac44-65da1fd68aea",
                "name": "Result2",
                "type": "string"
            },
            {
                "id": "5a3277d3-386d-4a12-b373-af38d91a727d",
                "name": "Result3",
                "type": "string"
            },
            {
                "id": "61fc107a-8929-4a33-8f26-2a0228344adc",
                "name": "Result4",
                "type": "string"
            },
            {
                "id": "598a1843-12f0-42ea-819d-944c1e1d6060",
                "name": "Result5",
                "type": "string"
            },
            {
                "id": "c6182b5f-df2f-4d6d-91b0-77e8679f052f",
                "name": "Result6",
                "type": "string"
            },
            {
                "id": "a368b7db-3d0c-4385-9f56-ad9793072b97",
                "name": "Result7",
                "type": "string"
            },
            {
                "id": "ae5db407-ce44-485c-a1a2-e0ae33353279",
                "name": "Result8",
                "type": "string"
            },
            {
                "id": "760bb1f1-70f6-49e9-a9b4-9e8d7e26b14a",
                "name": "Result9",
                "type": "string"
            }
        ],
        "instructions": null
    }
]

我正在尝试获取以下值:

I'm trying to get the value of:

"id": "598a1843-12f0-42ea-819d-944c1e1d6060",
                "name": "Result5",
                "type": "string"

我设法获取值。
现在,我有9个env变量,分别用于id,名称和类型,我想将值传递给它们,而不为每个变量创建设置事件。
env变量是:
varID1到varID9,
varName1到varName9,
varType1到varType9。

I managed a way to get the values. Now, i have 9 env variables for id, name and type and i want to pass the values to them without creating a set event for each. the env variables are: varID1 to varID9, varName1 to varName9, varType1 to varType9.

I'我尝试使用下面的代码,但是名称 varID ='varId'+ k创建的名称(例如,varID1)未在集合中使用。
i无法看到为什么这行不通。

I'm trying the bellow code but the name created by"varID ='varId'+k", e.g varID1 isn´t used on the set. i can´t see why this doesn´t work.

for ( var j = 0; j < test ; j ++ ) {
    var k = j + 1;
    var varID ='varId'+k;
    var varName ='varName'+k;
    var varType ='varType'+k;
    pm.environment.set(varID,  jsonData[key].variables[j].id);
    pm.environment.set(varName, jsonData[key].variables[j].name);
    pm.environment.set(varType, jsonData[key].variables[j].type);
    console.log(varID + " " + jsonData[key].variables[j].id );
    console.log(varName + " " + jsonData[key].variables[j].name);
    console.log(varType + " " + jsonData[key].variables[j].type);               
}

您有建议吗?

推荐答案

这是否可以帮助您实现同一目标?

Would this help you achieve the same thing? It could be cleaner but I think it would be the same.

let idCount = 1
let nameCount = 1
let typeCount = 1
_.each(_.first(pm.response.json()).variables, (arrItem) => {
    pm.environment.set(`varID${idCount ++}`,  arrItem.id);
    pm.environment.set(`varName${nameCount ++}`, arrItem.name);
    pm.environment.set(`varType${typeCount ++}`, arrItem.type);
})

它将存储像这样的变量:

It would store the variables like this:

这篇关于邮递员从数组中获取值并在env变量上进行动态设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆