Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ncaught TypeError:无法读取属性'';未定义的_Javascript_Reactjs - Fatal编程技术网

Javascript ncaught TypeError:无法读取属性'';未定义的

Javascript ncaught TypeError:无法读取属性'';未定义的,javascript,reactjs,Javascript,Reactjs,我正在尝试使用react向网页添加复选框。它的价值在于使用WalletCreadit。之后,通过提交表单,它应该将其发布到api.py文件。这是post方法: submitForm: (pay_method, useWalletCredit, onSuccess, onFailure) => { console.log(this.useWalletCredit); dispatch({type: 'START_LOADING'}); return PrivateAp

我正在尝试使用react向网页添加复选框。它的价值在于使用WalletCreadit。之后,通过提交表单,它应该将其发布到api.py文件。这是post方法:

submitForm: (pay_method, useWalletCredit, onSuccess, onFailure) => {
    console.log(this.useWalletCredit);
    dispatch({type: 'START_LOADING'});
    return PrivateApi.post('cart/pay', {
        'submit-pay': 'submit',
        web: 'true',
        'pay-method': pay_method,
        'useWalletCredit_': this.useWalletCredit,
    })
        .then(res => {
            dispatch({type: 'END_LOADING'});
            if (onSuccess) onSuccess(res.data);
        }).catch(error => {
            dispatch({type: 'END_LOADING'});
            if (onFailure) onFailure(error);
        })
},
它会引发以下错误:

CartPay.js:532未捕获类型错误:无法读取未定义的属性“useWalletCredit”


如果这是组件/类,并且您使用箭头函数,
将是
未定义的
,因为没有父上下文可供使用

使用正常功能:

submitForm: function(pay_method, useWalletCredit, onSuccess, onFailure) {
   ...
}

您正在使用箭头函数。箭头函数不创建自己的
对象,而是从其外部作用域继承

同样奇怪的是(对我来说,没有进一步的上下文),你没有任何
这个
对象可以使用,因为我希望
useWalletCredit
的值是
未定义的


不过,您不必有这个问题,因为您可以直接引用
useWalletCredit

submitForm
方法采用名为
useWalletCredit
的参数,但方法中的代码忽略该参数并使用
this.useWalletCredit
。为什么?老实说,我尽量避免问这个问题,因为我想先帮助解决这个问题。但是,是的,这是非常令人费解的。使用参数是比较容易做到的,也是非常直观的。useWalletCreadit在顶部的状态中定义。这就是为什么它需要这个,指向useWalletState@hereticMonkey如果useWalletCredit存储在状态中,为什么不删除submitForm上的参数?因为我需要将其发布到api.py@NathanAllusWealletCredit顶部的状态中定义的。这就是为什么它需要这个,指向useWalletState。请发布整个文件的内容,或者可能只是封闭组件的框架和状态。您使用的是类组件还是函数组件?我需要比您提供的更多的信息。此外,您必须从状态本身而不是从
this
对象访问状态属性。我不明白你怎么能做到这一点,因为
这个
是未定义的。我肯定需要更多信息。useWalletCreadit在顶部的州中定义。这就是为什么它需要这个,指向useWalletState。@MHB你能展示你的全部代码吗<代码>正如错误所说,此
是完全未定义的。