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

调用异步函数未加await,无法读取MatchList属性的问题排查

问题原因及解决方案

1. 异步函数调用未处理Promise

GetList是异步函数,直接执行let list = GetList(...)得到的是Promise对象,而非实际返回的接口数据。Promise对象本身没有SearchResult属性,所以直接访问list.SearchResult.MatchList会抛出Cannot read properties of undefined错误。

修复方式:

如果当前代码在async函数内部,用await获取结果:

// 确保当前代码包裹在async函数中
let list = await GetList(JSON.stringify(data), "");
console.log(list.res.SearchResult.MatchList); // 注意正确路径

或者用then链式调用处理异步结果:

GetList(JSON.stringify(data), "")
  .then(list => {
    console.log(list.res.SearchResult.MatchList);
  })
  .catch(err => console.error(err));

2. 数据嵌套路径错误

从petition函数的代码可以看到,它最终返回的是{res: result}结构,其中result才是接口返回的包含SearchResult的JSON数据。也就是说GetList返回的完整对象结构是:

{
  res: {
    SearchResult: { MatchList: [ [Object] ], numOfMatches: 1, totalMatches: 1 }
  }
}

你之前console.log(list)的输出可能是误写,或是已经取了list.res后的结果。实际正确的访问路径是list.res.SearchResult.MatchList,而非list.SearchResult.MatchList。

额外注意

  • 检查petition函数中JSON.parse(data.toString('utf8'))是否正确解析了接口返回数据,确保result确实包含SearchResult字段。
  • 当前petition的catch块仅打印异常,未向外抛出,建议在调用时添加错误捕获逻辑,避免静默失败。

内容的提问来源于stack exchange,提问作者jhon estrada

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 05:05:29