繁体   English   中英

Postman 脚本如果为 null 值的其他条件

[英]Postman Script if else condition for null values

我试图使用 if else 来访问 [ ] 条件

pm.test("Cases here", function () {
    var jsonData = pm.response.json();
    if (jsonData === null) {
        console.log('1');
        postman.setNextRequest("Get Count");
    }
    else {
        console.log('2');
        postman.setNextRequest("Create");
}
})

在此处输入图像描述

“我想要 Condition 为空时应该打印 1”

但我的反应是

在此处输入图像描述

如果你确定jsonData是一个数组,你可以检查它的长度

if (jsonData.length === 0) {
    // ...
}

如果你不确定它是一个数组,你可以确保然后检查它的长度:

if (Array.isArray(jsonData) && jsonData.length === 0) {
    // ...
}

Comparing it with null will always result in false , because the variable jsonData does not hold the value null , it holds an array, which is an object, and thus different from null .

您可以设置以下条件,因为它不是null它是一个empty array

pm.test("Cases here", function () {
  var jsonData = pm.response.json();
  if (!jsonData || !jsonData.length) { # see condition here
      console.log('1');
      postman.setNextRequest("Get Count");
  }
  else {
     console.log('2');
     postman.setNextRequest("Create");
  }
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM