React Saga Generator вызывает вызов неопределенного объекта - PullRequest
0 голосов
/ 29 октября 2019

Поэтому я использую axios, чтобы позвонить на мой сервер и получить ответ, и попробовал его с помощью redux-saga, но безуспешно. Когда я консольный журнал внутри моего вызова axios, я получил ответ, но signInUser в вызове yield неопределен навсегда. Что тут не так?

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 Ответ

1 голос
/ 29 октября 2019

Вы забыли вернуться в signInUserM, и, как мне кажется, впереди других ждут.

...