React Saga Generator yield调用未定义对象

时间:2019-10-29 19:16:38

标签: reactjs redux async-await axios redux-saga

因此,我正在使用axios来调用服务器并获得响应,并使用redux-saga进行了尝试,但未成功。当我在axios调用中进行控制台日志操作时,得到了响应,但是yield调用中的signInUser永远是不确定的。这里有什么问题吗?

const signInUserM = async (email, password) => {
  await axios
    .get("https://localhost:44320/Account/token")
    .then(async function(response) {
      const { data } = response;
      axios.defaults.headers.common = {
        Authorization: `Bearer ${data.token}`
      };

      await axios
        .post("https://localhost:44320/Login", {
          email: email,
          password: password
        })
        .then(authUser => {
          console.log(authUser); // got response
          return authUser;
        })
        .catch(error => {
          console.log(error);
          return error;
        });
    })
    .catch(error => {
      console.log(error);
      return error;
    });
};

function* signInUserG({ payload }) {
  const { email, password } = payload;
  try {
    const signInUser = yield call(
      signInUserM,
      email,
      password
    );
    console.log(signInUser); // undefined forever
    if (signInUser) {
      // never gets here
      yield put(userSignInSuccess(signInUser.id));
    }
  } catch (error) {
    console.log(error);
  }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您也忘记了在signInUserM和其他等待者的面前回来。