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

调用testFunction时报Cannot read properties of undefined (reading 'then')如何解决

报错原因

核心原因是testFunction没有统一返回Promise类型的结果,导致你调用.then()的目标值不符合预期:

  • 当user_email存在时,if分支内仅执行了sendEmail云函数,没有将sendEmail返回的Promise作为testFunction的返回值,此时testFunction的返回值为undefined,对undefined调用.then()就会触发你遇到的Cannot read properties of undefined (reading 'then')报错。
  • 当user_email不存在时,else分支返回的是布尔值true,布尔值属于原始类型没有.then()方法,走这个分支时同样会抛出类型错误。
修复方案

修改testFunction的逻辑,保证两个分支都返回Promise即可:

function testFunction(user_email, email_html) {
    if (user_email) {
        // 新增return,将云函数返回的Promise抛出
        return sendEmail({ recipient: user_email, subject: "Some words", body: email_html, html: true })
    } else {
        // 把布尔值包装为成功状态的Promise,保证返回值类型统一
        return Promise.resolve(true)
    }
}

修改后testFunction无论走哪个分支都返回Promise,就可以正常链式调用.then()和.catch()了。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 23:06:03