How to update global variable in Postman?

Poo*_*oja 2 postman

I have a test which Posts some items
e.g.
POST:item : 1
POST:item : 2 and so on

and now in 1st Post request, I am trying to set a global variable in pre-request-script

postman.setGlobalVariable("item", 1);

and use this variable in the body e.g

 "item": "{{item}}",
Run Code Online (Sandbox Code Playgroud)

it work.
Now in 2nd Post request, I wanted to increment global variable, in pre-request-script

item=item+1;
postman.setGlobalVariable("item", item);
Run Code Online (Sandbox Code Playgroud)

and in body same as above. but its give following error

There was an error in evaluating the Pre-request script: item is not defined

小智 7

使用当前版本的 Postman (6.2.x),您可以在 Tests 选项卡中设置变量,如

全局变量

pm.globals.set("variable_key", "variable_value");
Run Code Online (Sandbox Code Playgroud)

环境变量

pm.environment.set("variable_key", "variable_value");
Run Code Online (Sandbox Code Playgroud)

要更新答案,它将是

pm.globals.set("item", Number(postman.getGlobalVariable("item"))+1);
Run Code Online (Sandbox Code Playgroud)