FlutterLover
FlutterLover

Reputation: 671

Postman : TypeError: Cannot read property 'Response' of undefined

I want to extract a data from Postman response (in my test sNomPrimaire) to use it after as a variable, when i try to see the console log i get this error (TypeError: Cannot read property 'Response' of undefined).

This is my script

var jsonData = pm.response.json();
pm.globals.get(jsonData.results.Response.sNomPrimaire);
console.log("Test to get NomPrimaire:", jsonData.results.Response.sNomPrimaire);

This is my postman response :

{
"Response": {
    "id": "11452",
    "iSystExtUserId": null,
    "sCourriel": "[email protected]",
    "sIdentifiant": null,
    "sNomPrimaire": "testfirstname",
    "sNomSecondaire": "restlastname",
    "bOpen": false,
    "bVerified": false,
 
},
"InvalidApiVersion": 0,
"ErrorMessage": "",
"iHttpStatusCode": "201"
}

Upvotes: 1

Views: 12351

Answers (2)

Stroh
Stroh

Reputation: 45

Unless that's a dummy email address, you should remove it from your post.

Little late to this, but this is the approach I would take (not sure I completely understand the question, though.. I am assuming you are making some type of comparison between response bodies):

if (responseBody.length === 0) return console.warn("Empty response");

var res = JSON.parse(responseBody);

var initialResponse = pm.globals.get("initial_response");

if (typeof initialResponse !== "string") // it hasn't been set yet
{
    pm.globals.set("initial_response", res.Response.sNomPrimaire); // May need to be res["Response"].sNomPrimaire, as Danny pointed out
}
else
{
    makeSomeTypeOfComparison(initialResponse, res.Response.sNomPrimaire);
    pm.globals.unset("initial_response"); // if you want to reset your variables after one iteration
    // I advise using collection-level variables for things of this nature, though. e.g.
    // pm.collectionVariables.unset("initial_response");
}

It does seem like the presumed end-goal could be handled in a more productive way. Reach out if you need any help. I am quite versed in Postman. Try asking your question with more details so people can better help you.

Upvotes: 0

Danny Dainton
Danny Dainton

Reputation: 25851

You would need to use this instead of what you have there in your test:

pm.globals.set("var_name", jsonData.Response.sNomPrimaire)

Not sure where you got results from but it's not needed.

Upvotes: 2

Related Questions