簡體   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