ReferenceError: BasketID is not defined

Hi,

I am getting error “ReferenceError: BasketID is not defined”. I have created a Global Variable call “BasketID”. and during my test I am storing value from response against the variable.

After running script I can see the Global Variable is showing value, but in console I get the error. I am new to Postman and struggling to figure this out.

Thanks in advance…

=== Script=== Post Response ====
const response = pm.response.json();
const basketid = response[“basketId”];

pm.globals.set(“BasketID”,basketid);

console.log(BasketID);

pm.test("Basket Id is create ", function (){
pm.expect(pm.response[“basketId”]).to.not.be.null;
});

pm.test(“Successful POST request”, function () {
pm.expect(pm.response.code).to.be.oneOf([200,400]);
});

image

Hi @arshedahmed. Welcome to the Postman Community!

From the screenshot you shared, you declared a variable “basketid” on line 4, but you’re referencing a variable “BasketID” on line 7. The variables withing your script are local to your script and Global, Collections, and Environments variable cannot be accessed the same way you would access them.

To log a global variable to the console, you will need to modify line 7 to console.log(pm.globals.get("BasketID"))

1 Like

works fine… Thank you for your help

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.