You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Postman断言校验响应车型时报undefined错误应如何排查解决

Postman断言报错排查方案

错误根因

接口返回的pm.response.json()对应的值是数组类型,本身没有model属性,你在断言语句里直接调用response.model自然会返回undefined,和预期值Model 3比对就会触发你遇到的报错。
你前面的筛选逻辑是正确的,已经把匹配到的Model 3对应的对象存储到了model变量中,只需要修改断言语句的取值对象即可。

修复后代码

const response = pm.response.json()
 
let model;
 
for (let filter of response) {
    if (filter.model === "Model 3") {
       model = filter;
    }
}
console.log(model)
 
pm.test("car model is Model 3", function () {
    // 改用筛选出来的model变量取值
    pm.expect(model.model).to.eql("Model 3");
});

优化建议

可以直接用JS数组内置的find方法简化筛选逻辑,不需要手动写循环,代码更简洁:

const response = pm.response.json()
const targetCar = response.find(item => item.model === "Model 3")

pm.test("car model is Model 3", function () {
    // 新增非空判断,找不到对应车型时会优先抛出明确的错误提示
    pm.expect(targetCar, "未找到Model 3对应的车型数据").to.exist
    pm.expect(targetCar.model).to.eql("Model 3");
});

内容的提问来源于stack exchange,提问作者Wojciech Letynski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 12:06:03