问题描述
我在邮递员请求正文上具有这些属性
{
"firstName": "QA","lastName": "Test {{agent_phone_number}}","email": "qatest-{{agent_phone_number}}@mail.com"
}
并且我试图在“测试”选项卡上获取请求正文并将其设置在集合变量中,代码看起来像这样
const requestJson = JSON.parse(pm.request.body.raw);
pm.collectionVariables.set('agent_firstName',requestJson.firstName);
pm.collectionVariables.set('agent_lastName',requestJson.lastName);
pm.collectionVariables.set('agent_email'. requestJson.email);
当我运行它时,出现此错误
TypeError: Cannot read property 'email' of undefined
当我检查集合变量时,将保存名字和姓氏,但不会保存电子邮件。
“电子邮件”是否类似于Postman上的“ reserved”关键字?如果是这样,那么我如何从请求正文中获取“电子邮件”属性?
解决方法
您有一个。而不是错字:
pm.collectionVariables.set('agent_email'. requestJson.email);
应该是
pm.collectionVariables.set('agent_email',requestJson.email);