Environment variable ReferenceError Postman

时间:2018-06-04 16:59:31

标签: javascript json postman

I'm trying to pass a variable from JSON with Postman to an environment variable.

The variable is successfully extracted, but I can't seem to save it into the environment variable. My code

var allGazette = JSON.parse(responseBody);

if(allGazette.total_count!==0){
    for (i = 0; i < allGazette.total_count; i++) {
        var dateUse=allGazette.items[i].date;
        console.log(dateUse);
        postman.setEnvironmentVariable('jsonGazetteDate', dateUse);
        console.log(jsonGazetteDate);
    }
}
else(postman.setEnvironmentVariable('jsonGazetteDate',''));

My error is at the line starting postman.set... As per console logs below, dateUse successfully comes out as 2018-05-01. I've tried various combinations of stringify/parse but can't get this to work. Any ideas?

GET https://api.companieshouse.gov.uk...
2018-05-01
ReferenceError | jsonGazetteDate is not defined
GetGazette: ReferenceError: jsonGazetteDate is not defined

1 个答案:

答案 0 :(得分:1)

看起来jsonGazetteDate变量未声明,但您正在尝试将其记录到控制台。所以在循环数据时很可能会出错。

如果您尝试注销之前在该行中设置的环境变量,则需要执行以下操作:

console.log(pm.environment.get('jsonGazetteDate'))

此外,如果您使用的是本机客户端应用程序,我会更换旧的邮递员语法语句,转而使用较新的pm.*函数。

  • JSON.parse(responseBody)= pm.response.json()
  • postman.setEnvironmentVariable()= pm.environment.set()