【问题标题】:Could not call outside function from bolt Response Handler Callback无法从螺栓响应处理程序回调调用外部函数
【发布时间】:2020-08-08 21:36:56
【问题描述】:

我无法从 bolt responseHandler 调用我的 Update 方法:请帮助我解决这个问题。

角度 9:

错误:core.js:1673 错误类型错误:this.updatePaymentInfo 不是 Object.responseHandler 处的函数

updatePaymentInfo() 在同一个 ts 文件中声明。

请找到以下代码:

var RequestData={
    key: paymentRequest.key,
    txnid: paymentRequest.txnId,
    hash: paymentRequest.hash,
    amount: paymentRequest.amount,
    firstname: paymentRequest.firstName,
    email: paymentRequest.email,
    phone: paymentRequest.phone,
    productinfo: paymentRequest.productInfo,
    udf1: paymentRequest.udf1,
    surl: paymentRequest.sUrl,
    furl: paymentRequest.fUrl
    }
var Handlers={
        responseHandler: function(BOLT) {
               if (BOLT.response.txnStatus != "CANCEL") {
                this.updatePaymentInfo(BOLT.response);
               } 
               else {
                alert(BOLT.response);
               }
        return BOLT.response;
        },
        catchException: function(BOLT) {
          alert(BOLT.message);
        }
   }

    launchBOLT(paymentRequest) {
        bolt.launch(RequestData,Handlers);
    }

【问题讨论】:

    标签: angular scope closures payumoney


    【解决方案1】:

    如果您尝试访问内部函数范围之外的值,则应使用箭头函数。而不是:

    responseHandler: function(BOLT) {
        if (BOLT.response.txnStatus != "CANCEL") {
          this.updatePaymentInfo(BOLT.response);
        } else { alert(BOLT.response); }
        return BOLT.response;
      }
    ...
    

    使用箭头函数,以便您可以访问外部范围 this 对象:

    responseHandler: (BOLT) => {
        if (BOLT.response.txnStatus != "CANCEL") {
          this.updatePaymentInfo(BOLT.response);
        } else { alert(BOLT.response); }
        return BOLT.response;
      }
    ...
    

    否则,this 将引用匿名函数本身,而不是引用它当前所在的类。

    如果您使用的是 ECMAScript 3 或更早版本,则必须先将 this 保存在变量中,然后再在函数中使用它,因为这些版本中不存在箭头函数(正如我们所说关于 Angular 9,我认为情况并非如此):

    var _this = this;
    var Handlers = {
      responseHandler: function(BOLT) {
          if (BOLT.response.txnStatus != "CANCEL") {
            _this.updatePaymentInfo(BOLT.response);
          } else { alert(BOLT.response); }
          return BOLT.response;
        }
    ...
    

    【讨论】:

      猜你喜欢
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      • 1970-01-01
      • 2013-06-07
      • 2018-09-21
      • 1970-01-01
      相关资源
      最近更新 更多