Axios数据为未定义的错误

时间:2018-01-08 16:31:28

标签: javascript reactjs axios

我正在尝试通过axios发送一个帖子请求,并且我得到一个错误,说我通过的参数未定义。我已经通过调试验证了我传递给post函数的数据没有未定义且应该是正确的,所以我不明白为什么我会得到未定义的错误。

到目前为止,这是我的工作;

POST功能

export function userSignupPostRequest(userData){
    return dispatch => {

        return axios({
            method: 'post',
            url: BASE_URL + '/api/user/',
            data: {
                name: userData.username,
                passwordDigest: userData.passwordDigest,
                email: userData.email
            }
        });
    }
}

调用POST功能的事件

import User from "../../pojo/user";

.....

onSubmit(e) {
    e.preventDefault();

    if(this.isValid()){
        this.setState({ errors: {}, isLoading: true });
        var newUser = User(this.state.username, this.state.password, this.state.email);
        this.props.userSignupPostRequest(newUser)
            .then(
                () => {
                    this.props.addFlashMessage({
                        type: "success",
                        text: "You signed up successfully. Welcome!"
                    })
                    this.context.router.push("/");
                },
                ({ data }) => this.setState({ errors: data, isLoading: false })
            )
            .catch(e => {
                console.log(e);
            });
    }
}

用户类

var bcrypt = require('bcryptjs');

const SALT_SIZE = 10;

export function encryptPassword(password){
    return bcrypt.hashSync(password, SALT_SIZE);
}

export default function User(username, password, email){
    var passwordDigest = encryptPassword(password);

    return {
        username: username,
        passwordDigest: passwordDigest,
        email: email
    }
}

错误

TypeError: Cannot read property 'username' of undefined
at SignupForm.render (SignupForm.js?2314:73)
at ReactCompositeComponentWrapper._renderValidatedComponentWithoutOwnerOrContext (ReactCompositeComponent.js?195a:815)
at ReactCompositeComponentWrapper._renderValidatedComponent (ReactCompositeComponent.js?195a:841)
at ReactCompositeComponentWrapper._updateRenderedComponent (ReactCompositeComponent.js?195a:773)
at ReactCompositeComponentWrapper._performComponentUpdate (ReactCompositeComponent.js?195a:753)
at ReactCompositeComponentWrapper.updateComponent (ReactCompositeComponent.js?195a:672)
at ReactCompositeComponentWrapper.performUpdateIfNecessary (ReactCompositeComponent.js?195a:585)
at Object.performUpdateIfNecessary (ReactReconciler.js?3d8b:160)
at runBatchedUpdates (ReactUpdates.js?371c:151)
at ReactReconcileTransaction.perform (Transaction.js?fbf2:136)

正如我所说,我不明白为什么会收到此错误,因为我可以验证userData的{​​{1}}未定义。任何帮助表示赞赏。

0 个答案:

没有答案