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

Redux Saga call方法报fn为undefined/null问题求解

报错原因

你遇到的Error: call: argument fn is undefined or null由两个核心问题导致:

  1. redux-saga的call效应用法错误:call的第一个参数要求传入函数引用本身,你当前写法call(register(/*参数*/))是直接先执行了register函数,把函数执行后的返回值传给了call。
  2. 你定义的register函数没有写返回值,默认返回undefined,call拿到的第一个参数是undefined,直接触发报错。

除此之外你的register函数里axios请求没有加await、也没有返回Promise实例,就算修正call的写法也拿不到正确的接口响应结果,异常捕获逻辑也会失效。

修复步骤

1. 修正saga中call的调用方式

call的标准传参格式为call(目标函数, 参数1, 参数2, ...),不要在call参数位直接执行目标函数,同时确认文件顶部正确引入了register方法。
修正后的auth.saga.js代码:

function* asyncRegistration(action) {
  const {
    orgName,
    orgUrl,
    timeZone,
    adminName,
    adminEmail,
    adminphone,
    password,
  } = action.payload;

  const result = yield call(
    register,
    orgName,
    orgUrl,
    timeZone,
    adminName,
    adminEmail,
    adminphone,
    password
  );

  console.log("saga", result);
}

export function* watchEveryAuth() {
  yield takeEvery(REGISTRATION_CREDENTIALS, asyncRegistration);
}

记得在文件顶部补充register的导入语句,路径按你实际项目的文件结构填写,示例:import { register } from './register',漏导入也会触发同样的报错。

2. 修正register.js的接口请求逻辑

给函数加上async关键字,等待axios请求完成后返回结果,同时抛出异常方便saga侧做后续错误处理:

import axios from "axios";

export const register = async (
  orgName,
  orgUrl,
  timeZone,
  adminName,
  adminEmail,
  adminphone,
  password
) => {
  try {
    const res = await axios.post(
      "http://amnius-connect.ebizontech.biz/amnius-connect/api/admin-registration",
      {
        organization_name: orgName,
        organization_website: orgUrl,
        time_zone: timeZone,
        username: adminName,
        email: adminEmail,
        phone: adminphone,
        password: password,
      }
    );
    console.log("our result is ", res);
    return res.data;
  } catch (e) {
    console.log(e);
    throw e;
  }
};

如果你不想用async/await语法,直接return axios.post(/*请求配置*/)也可以,saga的call会自动等待Promise状态变更后再恢复执行生成器函数。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 18:48:28