调度程序尝试获取后给出错误

时间:2019-11-03 21:01:05

标签: javascript reactjs react-native react-redux react-thunk

我正在尝试通过获取对Rest API的POST请求来登录用户。我使用调度程序,但出现错误:

Action must be plain objects. Use custom middleware for async actions.

我尝试添加redux-thunk,但未成功。

action.js

export const SIGNUP = 'SIGNUP';

export const signup = (email, password) => {
    return async dispatch => {
        const response = await fetch(
            `https://api.example.com/auth/`,
            {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    email: email,
                    password: password,
                    token: true
                })
            }
        );

        if (!response.ok) {
            throw new Error('Something went wrong!');
        }

        const resData = await response.json();
        console.log(resData)
        dispatch({ type: SIGNUP })
    };
};

为什么我会收到错误消息?怎样才能发送POST请求?

0 个答案:

没有答案