chargebee / chargebee-typescript

Typescript library for the Chargebee API.
https://apidocs.chargebee.com/docs/api?lang=typescript
MIT License
22 stars 16 forks source link

resources[this.sub_types[k]] is not a constructor #33

Closed markomitranic closed 1 year ago

markomitranic commented 1 year ago

This morning, I started seeing this error coming out of chargebee-typescript. More specifically:

TypeError: resources[this.sub_types[k]] is not a constructor
    at Subscription.load (/node_modules/chargebee-typescript/lib/resources/model.js:53:39)
    at new Model (/node_modules/chargebee-typescript/lib/resources/model.js:28:14)
    at new Subscription (/node_modules/chargebee-typescript/lib/resources/subscription.js:7:1)
    at Result.get (/node_modules/chargebee-typescript/lib/result.js:284:16)
    at Result.get subscription [as subscription] (/node_modules/chargebee-typescript/lib/result.js:26:34)
...

I'm seeing this on the subscription API endpoint and only sometimes, seems on certain entities. I was unable to find out what is wrong with them more specifically, so I ended up replacing result.subscription with JSON.parse(String(result))["subscription"] :P not ideal, by no means.

I was hoping to get any insight from you what might be the issue. I'm on the latest npm version.

lightness commented 1 year ago

Any updates?

cb-khushbubibay commented 1 year ago

Hi @markomitranic @lightness

I tried below sample codes and these are working fine. Please try this once. To Retrieve Subscription :

import { ChargeBee, _subscription } from 'chargebee-typescript'; var chargebee = new ChargeBee(); chargebee.configure({site : "site", api_key : "api-key"}); chargebee.subscription.retrieve("subs_id").request(function(error,result) { if(error){ //handle error console.log(error); }else{ console.log(${result.subscription}); var subscription: typeof chargebee.subscription = result.subscription; var customer: typeof chargebee.customer = result.customer; var card: typeof chargebee.card = result.card; } });

To Create Subscription :

import { ChargeBee, _subscription } from 'chargebee-typescript'; var chargebee = new ChargeBee(); chargebee.configure({site : "site", api_key : "api-key"}); chargebee.subscription.create_with_items("cust_id",{ subscription_items : [ { item_price_id : "itemPriceId", billing_cycles : 2, quantity : 1 }] }).request(function(error,result) { if(error){ //handle error console.log(error); }else{ console.log(${result}); var subscription: typeof chargebee.subscription = result.subscription; console.log(${result.subscription}); var customer: typeof chargebee.customer = result.customer; var card: typeof chargebee.card = result.card; var invoice: typeof chargebee.invoice = result.invoice; var unbilled_charges: Array = result.unbilled_charges; } });

If this will not work for you, could you please provide more context on the issue and provide us the code that you are trying to run.

lightness commented 1 year ago

Same result.

Exact script was:

/* eslint-disable @typescript-eslint/no-var-requires */
const { ChargeBee } = require('chargebee-typescript');

const chargebee = new ChargeBee();
chargebee.configure({
  site: '<SITE>',
  api_key: '<API-KEY>',
});

chargebee.subscription.retrieve('77CnlTW8wTPg5i4q').request(function (error, result) {
  if (error) {
    console.log(error);
  } else {
    console.log(`${result.subscription}`);
    const subscription = result.subscription;
    const customer = result.customer;
    const card = result.card;

    console.log('>>>', subscription, customer, card);
  }
});

Result:

/path/project/node_modules/chargebee-typescript/lib/resources/model.js:53
                        set_vals.push(new (resources)[this.sub_types[k]](item));
                                      ^

TypeError: resources[this.sub_types[k]] is not a constructor
    at Subscription.load (/path/project/node_modules/chargebee-typescript/lib/resources/model.js:53:39)
    at new Model (/path/project/node_modules/chargebee-typescript/lib/resources/model.js:28:14)
    at new Subscription (/path/project/node_modules/chargebee-typescript/lib/resources/subscription.js:7:1)
    at Result.get (/path/project/node_modules/chargebee-typescript/lib/result.js:284:16)
    at Result.get subscription [as subscription] (/path/project/node_modules/chargebee-typescript/lib/result.js:26:34)
    at /path/project/scripts/test.js:14:27
    at Timeout._onTimeout (/path/project/node_modules/chargebee-typescript/lib/util.js:92:21)
    at listOnTimeout (internal/timers.js:557:17)
    at processTimers (internal/timers.js:500:7)

Please, note, that issue is not reproduced for some other subscriptions. But for this subscription it's reproduced consistently

cb-khushbubibay commented 1 year ago

@lightness What version you currently using of chargebee-typescript?

lightness commented 1 year ago

Latest one: 2.17.0. With 2.16.0 also reproduced

cb-khushbubibay commented 1 year ago

@lightness Could you please Raise a support ticket for this, with all the relevant details. So we can meet over a call to debug this.

cb-khushbubibay commented 1 year ago

Hi @lightness @markomitranic This issue has been fixed in v2.17.1. Requesting you to please use the latest version.

lightness commented 1 year ago

It's fixed! Thanks!