[英]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).我想从 Postman 响应中提取数据(在我的测试 sNomPrimaire 中)以将其用作变量,当我尝试查看控制台日志时,我收到此错误(类型错误:无法读取未定义的属性“响应”)。
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": "teste@test.com",
"sIdentifiant": null,
"sNomPrimaire": "testfirstname",
"sNomSecondaire": "restlastname",
"bOpen": false,
"bVerified": false,
},
"InvalidApiVersion": 0,
"ErrorMessage": "",
"iHttpStatusCode": "201"
}
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.不知道你从哪里得到results
,但它不是必需的。
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.我对 Postman 非常精通。 Try asking your question with more details so people can better help you.试着用更多的细节来问你的问题,这样人们就可以更好地帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.