ngx-stripe: TypeError: Cannot read property 'elements' of undefined

Hello. I am currently facing an issue of having the stripe being undefined and the form elements go missing when the website is live (on https). All was good when I run on the localhost.

On live website 1

On localhost 2

Error on console: TypeError: Cannot read property 'elements' of undefined

3

Versions I am using angular: 7.1.1 ngx-stripe: ^7.6.0 stripe: ^8.39.1

app.module.ts

imports: [
   NgxStripeModule.forRoot(<my-stripe-pk>)
]

checkout.component.html

<form id="card-form" novalidate (ngSubmit)="stripePayment($event)" [formGroup]="stripeForm">
    <input type="text" class="form-control" formControlName="name" placeholder="Name on card">
    <ngx-stripe-card [options]="cardOptions" [elementsOptions]="elementsOptions"></ngx-stripe-card>
    <button type="submit" id="payment-btn" class="btn btn-success">Make Payment</button>
</form>

checkout.component.ts

import { StripeService, StripeCardComponent, ElementOptions, ElementsOptions } from "ngx-stripe";

@ViewChild(StripeCardComponent) card: StripeCardComponent;

stripeForm: FormGroup;

elementsOptions: ElementsOptions = {
  locale: 'en'
};

cardOptions: ElementOptions = {
    style: {
      base: {
        iconColor: '#666EE8',
        color: '#31325F',
        lineHeight: '40px',
        fontWeight: 300,
        fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
        fontSize: '18px',
        '::placeholder': { color: '#CFD7E0' }
      }
    }
};

constructor(private formBuilder: FormBuilder, private stripeService: StripeService) { }

ngOnInit() {
    this.stripeForm = this.formBuilder.group({
      name: ['', [Validators.required]]
    });
}

stripePayment( ) {
    this.stripeService.createToken(this.card.getCard(), { name }).subscribe(result => {
        if (result.token) {
          // do something
       }
    });
}

Any help will be greatly appreciated!

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 22 (5 by maintainers)

Most upvoted comments

@alphacentauridigital @richnologies My <ngx-stripe-card> is inside a conditional parent fieldset, which seems to be what’s causing the issue.

I was having this issue since switching to using StripeFactoryService to initialize instead of ForRoot.

I was using stripe connect to feed in a stripeAcocunt parameter into the intialization. The value is correct and so is the public key but for some resason I’m still getting …

Cannot read properties of undefined (reading 'elements')

…whenever I tried to render the component.

For me I forgot to feed the stripe instance into the component.

image

@alphacentauridigital

Turns out our Stripe Public key was not making in correctly after an update. This actually fixed it now. Thanks!

Ah, got it, I was using NgxStripeModule.forChild(), thinking it’s the one exporting ngx-stripe components, but it’s just NgxStripeModule without anything.

Thanks but no I meant that this was causing the issue, the load order was wrong. I already fixed it. I thought it could be a hint for others wo have this issue.