postman 如何在响应代码为200时中断for循环

voj3qocg  于 2023-02-19  发布在  Postman
关注(0)|答案(1)|浏览(212)

在下面的脚本中,我试图在获得状态码200后中断for循环,但即使在收到状态码200后,请求仍会继续发生。

var getLibraries = host+'webcontents/libraries';

pm.sendRequest(getLibraries, function (err, response) {
    var jsonData = response.json();
    var library_id;
    var statusCode;
    var length = jsonData.contents.length;

    for (var i = 0; i < length; i++ ){

        library_id = jsonData.contents[i].id;
        var getMLSConfigURL = host+'/library/'+library_id;
        pm.sendRequest(getMLSConfigURL, function (err, response) {
           statusCode = response.code;
            if(statusCode == 200){
                var mlsConfigData = response.json();
                console.log('ID: ', mlsConfigData.id);
                pm.environment.set("MLSConfigLib_Id", mlsConfigData.id);
                return;
            }
        });
        if(statusCode == 200){
            break;
       }
    }
});
92dk7w1h

92dk7w1h1#

如果你有你想要的条件,在FOR LOOP结束之前,把“i”变量设置为条件之后的长度(i = length),它将停止循环。

相关问题