【问题标题】:Their was an error in evaluation the Pre-request Script: Cannot read property 'get' of undefined他们在评估预请求脚本时出错:无法读取未定义的属性“获取”
【发布时间】:2019-03-10 22:16:28
【问题描述】:

我正在尝试通过预请求脚本在 Postman 中访问我的 POST 请求的响应。

下面给出的脚本:

var mobiles = postman.environment.get("mobiles");

if (!mobiles)
  {
    mobiles =["8824444866","8058506668"];  
  }

var currentMobile = mobiles.shift();
postman.environement.set("mobile","currentMobile");
postman.environement.set("mobiles",mobiles);

当我点击发送按钮时,在邮递员中它显示一个错误,如下所示:

他们在评估预请求脚本时出错:无法读取 未定义的属性“获取”。

请提出一些解决方案

【问题讨论】:

  • 显然environment 在这里是未定义的,console.log(postman) 向您显示了什么?

标签: javascript postman


【解决方案1】:

您尝试/寻找的是postman.setEnvironmentVariable(key, value),您可以使用新的pm.environment.set(key, value),当然它是相同功能的简写。

pm 可以访问环境API,而不是postman。 另外,您的脚本中有错字。 它是environment,而您在脚本中设置变量的位置是environement

这是固定的脚本:

注意:这仅适用于本机应用

var mobiles = pm.environment.get("mobiles");

if (!mobiles)
  {
    mobiles =["8824444866","8058506668"];  
  }

var currentMobile = mobiles.shift();
pm.environment.set("mobile", currentMobile);
pm.environment.set("mobiles", mobiles);

如果您想使用postman.* API,那么脚本将是:

适用于 Chrome 应用和原生应用(旧版支持)

var mobiles = postman.getEnvironmentVariable("mobiles");

if (!mobiles)
  {
    mobiles =["8824444866","8058506668"];  
  }

var currentMobile = mobiles.shift();
postman.setEnvironmentVariable("mobile", currentMobile);
postman.setEnvironmentVariable("mobiles", mobiles);

【讨论】:

  • 仍然面临这个问题,
  • 它显示他们在评估预请求脚本时出错:未定义 pm。
  • @AmitSheoran 我认为您正在使用 chrome 应用程序。使用我为 chrome 应用程序发布的第二个脚本。但是,我强烈建议您使用 Native App。 chrome 应用已被弃用。
  • 也请帮忙
  • @AmitSheoran 此外,您可以联系 Postman 社区,因为我们的整个团队和社区都在不断地互相帮助。这里:community.getpostman.com/latest
猜你喜欢
  • 2018-07-03
  • 2020-12-12
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
  • 2021-08-23
  • 2021-12-10
  • 2020-03-21
  • 1970-01-01
相关资源
最近更新 更多