简体   繁体   English

Uncaught (in promise) TypeError: _this is not a function

[英]Uncaught (in promise) TypeError: _this is not a function

I'm making a simple axios call and on success, call a toast.我正在打一个简单的 axios 电话,如果成功,请敬酒。 The toast function is also there and working fine because I am using it at 100 different places.吐司 function 也在那里并且工作正常,因为我在 100 个不同的地方使用它。

console log shows {data: "Payment Successful", status: 200, statusText: "OK", headers: {…}, config: {…}, …} means the call is working fine, it's just the this is not...控制台日志显示{data: "Payment Successful", status: 200, statusText: "OK", headers: {…}, config: {…}, …}表示调用正常,只是不是.. .

this.razorPayOptions = {
    'key': process.env.MIX_RAZORPAY_KEY,
    'name': process.env.MIX_APP_NAME,
    'description': 'Details ...',
    'handler': function (request) {
        axios.post('/api/transaction/complete', request)
            .then(response => {
                console.log(response);
                this.toast(response.data, response.status); // this is where the error occurs
            });
    }
}

Error : Uncaught (in promise) TypeError: _this.toast is not a function错误:未捕获(承诺中)类型错误:_this.toast 不是 function

I've searched on internet for past 2 days but couldn't find anything.我在互联网上搜索了过去 2 天,但找不到任何东西。 Some suggested to use async/await but no luck either.有些人建议使用 async/await 但也没有运气。 As always, stack overflow is my last resort.与往常一样,堆栈溢出是我最后的手段。 If you need more info, please add a comment below.如果您需要更多信息,请在下面添加评论。

Thank You.谢谢你。

Switch your handler to an arrow function to avoid changing the scope:将您的处理程序切换到箭头 function 以避免更改 scope:

this.razorPayOptions = {
    'key': process.env.MIX_RAZORPAY_KEY,
    'name': process.env.MIX_APP_NAME,
    'description': 'Details ...',
    'handler': (request) => {
        axios.post('/api/transaction/complete', request)
            .then(response => {
                console.log(response);
                this.toast(response.data, response.status); // this is where the error occurs
            });
    }
}

Check out this answer for more context查看此答案以获取更多上下文

You can either switch to an arrow function, or capture this before you define your callback:您可以切换到箭头 function,或者在定义回调之前捕获this

this.razorPayOptions = {
    'key': process.env.MIX_RAZORPAY_KEY,
    'name': process.env.MIX_APP_NAME,
    'description': 'Details ...',
    'handler': function (request) {
        const that = this;
        axios.post('/api/transaction/complete', request)
            .then(response => {
                console.log(response);
                that.toast(response.data, response.status); // this is where the error occurs
            });
    }
}

The problem is that you are misreading the scope.问题是您误读了 scope。 To solve your problem, change the function for an arrow function.要解决您的问题,请将 function 更改为箭头 function。

this.razorPayOptions = {
'key': process.env.MIX_RAZORPAY_KEY,
'name': process.env.MIX_APP_NAME,
'description': 'Details ...',
'handler': (request) => {
    axios.post('/api/transaction/complete', request)
        .then(response => {
            console.log(response);
            this.toast(response.data, response.status); // this is where the error occurs
        });
   }
}

could you read this post https://www.codementor.io/@dariogarciamoya/understanding-this-in-javascript-with-arrow-functions-gcpjwfyuc .你能读一下这篇文章https://www.codementor.io/@dariogarciamoya/understanding-this-in-javascript-with-arrow-functions-gcpjwfyuc 吗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获(承诺)TypeError:$不是函数 - Uncaught (in promise) TypeError: $ is not a function Uncaught (in promise) TypeError: t is not a function - Uncaught (in promise) TypeError: t is not a function Uncaught (in promise) TypeError: X is not a function - Uncaught (in promise) TypeError: X is not a function Uncaught (in promise) TypeError: $.templates is not a function - Uncaught (in promise) TypeError: $.templates is not a function 未捕获(承诺)TypeError:_this.tasks.push不是函数 - Uncaught (in promise) TypeError: _this.tasks.push is not a function Uncaught (in promise) TypeError: existingData.push is not a function - Uncaught (in promise) TypeError: existingData.push is not a function 未捕获(承诺)TypeError :(中间值).setAttribute不是函数 - Uncaught (in promise) TypeError: (intermediate value).setAttribute is not a function 未捕获(承诺)TypeError:$ scope.GetNomePacienteIndicou不是函数 - Uncaught (in promise) TypeError: $scope.GetNomePacienteIndicou is not a function Uncaught (in promise) TypeError: this.getChildrem is not a function - Uncaught (in promise) TypeError: this.getChildrem is not a function Uncaught (in promise) TypeError: res.map is not a function - Uncaught (in promise) TypeError: res.map is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM