Postman API学生专家认证技能检查报错:无法读取undefined的filter属性
问题:Postman认证技能检查报错"Cannot read properties of undefined (reading 'filter')"
我在制作用于Postman API Fundamentals Student Expert认证的集合时,需完成一项技能检查:在Tests标签脚本中使用pm.collectionVariables.set(...)将favoriteActor设为集合变量。但运行测试和提交认证时均触发相同错误:
Postman Test Script Error: "Cannot read properties of undefined (reading 'filter')"
提交认证后返回的错误详情:
{ "status": "error", "message": "Your collection failed one or more Postman tests. See the errors array for details.", "errors": [ { "test": "6. [skillcheck] Set \"favoriteActor\" as a collection variable using pm.collectionVariables set(...) in Tests tab script", "message": "Cannot read properties of undefined (reading 'filter')" } ] }
我的测试脚本代码如下:
pm.test(`6. [skillcheck] Set "favoriteActor" as a collection variable using pm.collectionVariables.set(...) in Tests tab script`, function () { pm.collectionVariables.set("favoriteActor", "Tony Stark"); const favoriteActor = pm.collectionVariables.get("favoriteActor"); pm.expect(favoriteActor).to.equal("Tony Stark"); });
解决方案
这个错误并非你的代码逻辑问题,而是认证系统校验脚本的执行时机导致的,可通过调整代码结构规避:
- 核心调整:不要将
pm.collectionVariables.set()嵌套在pm.test()的回调函数内,直接放在Tests标签的全局脚本区域(最外层) - 可选保留断言逻辑(认证系统会自行校验变量是否设置,也可以去掉自己的断言)
调整后的代码示例:
// 直接在Tests标签最外层设置集合变量,避免嵌套在测试用例中 pm.collectionVariables.set("favoriteActor", "Tony Stark"); // 可选:自行添加验证用例(非认证强制要求) pm.test(`Verify favoriteActor variable is set correctly`, function () { const favoriteActor = pm.collectionVariables.get("favoriteActor"); pm.expect(favoriteActor).to.equal("Tony Stark"); });
原因说明
认证系统的校验脚本会在你的测试用例执行前尝试读取集合变量相关属性,嵌套在pm.test()里的代码执行时机偏晚,导致校验时变量尚未被设置,进而触发filter相关的未定义错误。
内容的提问来源于stack exchange,提问作者Prabhat Kumar
相关产品推荐
相关产品推荐

