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

Redux-Saga的call effect调用翻译API成功却始终进入catch块如何解决

问题根因

你代码里的redux-saga call effect用法错误,导致执行逻辑异常抛出错误,必然进入catch块。
call的标准用法是第一个参数传入待执行的函数引用,后续参数依次传入该函数的入参,而你现在的写法是直接调用了translateText,将它返回的Promise对象传给了call,call尝试把Promise作为函数执行时就会抛出错误。


修复方案

把saga中调用translateText的代码改成正确的call传参格式即可:

// 错误写法
// const translatedProtection: ReturnType<typeof translateText> = yield call(translateText(data.adequate_protection, currentLanguage.replace(/[^a-z]/g, '')))

// 正确写法
const translatedProtection: ReturnType<typeof translateText> = yield call(
  translateText, 
  data.adequate_protection, 
  currentLanguage.replace(/[^a-z]/g, '')
)

后续排查步骤

如果修改后还有异常,可以按以下顺序定位问题:

  • 打印currentLanguage.replace(/[^a-z]/g, '')的输出,确认传给翻译API的目标语言代码符合Google Translate API的规范,避免传入无效的语言标识
  • 确认data.adequate_protection的值不是undefined或null,避免给翻译API传入无效的待翻译内容
  • 在saga的catch块中打印error的具体内容,确认报错来源,避免被通用报错提示掩盖真实问题

内容的提问来源于stack exchange,提问作者Felipe Evangelista

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 19:06:00