Postman执行脚本报ReferenceError: list is not defined错误如何解决
报错根因
JavaScript 中,当对象的属性名包含减号(-)这类特殊字符时,不能直接使用点运算符访问属性。你代码中的response.provider-list会被 JS 引擎解析为response.provider 减去 list的运算表达式,由于你没有定义过名为list的变量,就会抛出ReferenceError: list is not defined的错误。
额外说明:Postman 提供的pm.response.json()方法本身就会自动解析当前请求的响应体,不需要额外传入responseBody参数,多余传参可能会导致解析异常。
解决方案
所有包含特殊字符的属性名,改用方括号包裹字符串属性名的方式访问即可,和你使用的JSON路径规则完全对应。
修正后完整代码
response = pm.response.json() console.log(response) token = response["provider-list"][6]["user-list"][1]["activation-token"] console.log(token)
可选验证步骤
如果你担心数组下标越界,可以先加日志确认对应层级的内容是否存在:
console.log(response["provider-list"]?.length) console.log(response["provider-list"]?.[6]?.["user-list"]?.length)
内容的提问来源于stack exchange,提问作者Fly High
相关产品推荐
相关产品推荐

