邮递员从数组中获取值并在env变量上进行动态设置

时间:2018-08-09 21:10:37

标签: postman

[
    {
        "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
    }
]

我正在尝试获取以下值:

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

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

我正在尝试以下代码,但是“ varID ='varId'+ k”创建的名称(例如varID1)未在电视机上使用。 我看不到为什么这行不通。

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);               
}

您有建议吗?

1 个答案:

答案 0 :(得分:1)

这是否可以帮助您实现同一目标?可能更干净,但我认为会一样。

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);
})

它将存储这样的变量:

enter image description here