首页 > 解决方案 > 使用函数时无法从 Angular 发布到 API

问题描述

我正在尝试在回调函数中发布付款后生成的令牌,但问题是我无法在回调函数中发布该令牌,因为它无法识别HttpClient.

代码如下。

  async makePayment() {
      var token;
      var obj;
      var yoco = new window.YocoSDK({
    publicKey: 'xxxxxxxxxxxxxxxxxxxx',
    locale: 'auto',
    });
       await yoco.showPopup({
            amountInCents: 300,
            currency: 'ZAR',
            name: 'Your Store or Product',
            description: 'Awesome description',
            callback: await function (result) {
              if (result.error) {
                const errorMessage = result.error.message;
                alert("error occured: " + errorMessage);
              } else {
                alert("card successfully tokenised: " + result.id);
                token=result.id;
                obj = {
                    "token" : token,
                    "amountInCents" : 300,
                    "currency" : "ZAR"
                }
                this.resumeGeneratorService.PaymentCONFIRMMTF().subscribe(x=>{
                    var confirmationres = x;
                })
              }
            }
        });
  }
  async payment(){
      console.log(async);
    await this.makePayment();
    console.log("result");
    
    this.resumeGeneratorService.PaymentCONFIRMMTF().subscribe(x=>{
        var confirmationres = x;
    })
  }
  invokePayment() {
    if(!window.document.getElementById('stripe-script')) {
      const script = window.document.createElement("script");
      script.id = "stripe-script";
      script.type = "text/javascript";
      script.src = "https://js.yoco.com/sdk/v1/yoco-sdk-web.js";
      script.onload = () => {
        this.paymentHandler = (<any>window).YocoSDK
      }
      window.document.body.appendChild(script);
    }
}

如果我在函数中调用 POST 方法,则会收到以下错误:

zone-evergreen.js:171 Uncaught TypeError: Cannot read property 'request' of undefined
at Object.yoco.showPopup.callback (build-cv.page.ts:359)
at Object.onSuccess (Checkout.tsx:32)
at index.tsx:59
at ZoneDelegate.invokeTask (zone-evergreen.js:399)
at Zone.runTask (zone-evergreen.js:167)
at invokeTask (zone-evergreen.js:480)
at ZoneTask.invoke (zone-evergreen.js:469)
at timer (zone-evergreen.js:2552)

MakePayment()正在拥有需要发送回 API 的令牌。有人可以帮我吗?

标签: javascriptangularrest

解决方案


推荐阅读