Can any one let me know why i am getting the below error as undefined

Request body:

let response = pm.response.json();

pm.environment.set("id", response.id)

pm.environment.set("token", response.createdAt)

pm.test('Verifying the ID Value', function(){

pm.expect(response.id).is.eql(pm.environment.get('id'));

pm.expect(response.createdAt).is.eql(pm.environment.get('token'));

});

console.log(pm.environment.get('id'));

console.log(pm.environment.get(pm.environment.get('token')));

Request in Post: https://reqres.in/api/users

Respose body:

{

    "name": "morpheus",
    "job": "leader",
    "id": "808",
    "createdAt": "2020-09-08T11:54:02.679Z"
}

I have created 2 environment variable ID and token for ID and Createdat response. Please help me out on this

Hello @shota331 you are still having this issue? Usually undefined is displayed when you add the parameters and there is no values defined it under any level (Global, environment etc…)

May be the below blog will be useful:

I ran the request with the test script you provided. I’m going to guess that you’re just talking about the console log saying undefined.

The blog post that @bpricilla provided will probably help on understanding what variables are. I can’t quite follow what you’re trying to do.

Problems that I see:

  • You are trying to grab values that don’t exist
  • You are testing that a variable is equal to itself
  • You are trying to console log variables that don’t exist.
  • There is no createdAt property in the response

Here is what I think you’re trying to do:

const response = pm.response.json();

const ids = _.map(response.data, 'id');
pm.test('Values are returned with Ids', function(){
  pm.expect(ids.length).to.be.at.least(0);

  pm.environment.set('ids', JSON.stringify(ids));
});

console.log(ids);