Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Cannot read property 'MessageState' of undefined #242

Open
alecrobertson1 opened this issue Mar 30, 2023 · 7 comments
Open

[WIP] Cannot read property 'MessageState' of undefined #242

alecrobertson1 opened this issue Mar 30, 2023 · 7 comments

Comments

@alecrobertson1
Copy link

alecrobertson1 commented Mar 30, 2023

I am using node-mailjet within a node project that is the basis of an AWS lambda.

I am importing the Message namespace from the node-mailjet package as follows:

import { Message } from 'node-mailjet'

When the application runs (transpiled using Webpack), Message is undefined and so when I refer to the MessageState enum on this namespace, the application fails. I require the MessageState enum so I can do error handling and logging.

This appears to be an issue with how node-mailjet is processing its namespaces, with the transpiled code not including this namespace or any of its exports and so they come back as undefined.

@alecrobertson1
Copy link
Author

alecrobertson1 commented Mar 30, 2023

I seem to have located the problem:

Only the default export is being exported, even though the types - including from within the Message namespace - are being exported from the index when declared in TypeScript:

export * from './types/api';
export { Client, Request, HttpMethods };
export default Mailjet;

I saw in the commit history that at one point it was considered to remove this line and it seems to have not been done. Why has this decision only been taken for the browser and not the Node version? Would it not be useful for these types to be available?

@ai-wintermute
Copy link
Collaborator

Hi @alecrobertson1 I'm not sure why it was done. But I've tried the import you mentioned and it works. Can you please share the whole code snippet?

@alecrobertson1
Copy link
Author

Hi @ai-wintermute I have created a very simple test project to demonstrate the problem, it is available at https://github.com/alecrobertson1/TestProject.

If you run the main file using node dist/main.js you will see the error as above:

TypeError: Cannot read property 'MessageState' of undefined

@trevorblades
Copy link

trevorblades commented Jun 4, 2023

I'm running into the same problem here, trying to implement a simple subscribe/unsubscribe function in TypeScript:

import Mailjet, { BulkContactManagement } from "node-mailjet";
import type { ContactSubscription } from "node-mailjet";

const mailjet = new Mailjet({
  apiKey: process.env.MAILJET_API_KEY,
  apiSecret: process.env.MAILJET_API_SECRET,
});

const subscribeToNewsletter = (id: number, shouldSub = true) => {
  const contactLists: ContactSubscription.PostContactManageContactsListsBody = {
    ContactsLists: [
      {
        ListID: MAILJET_NEWSLETTER_ID,
        Action: shouldSub
          ? BulkContactManagement.ManageContactsAction.AddForce
          : BulkContactManagement.ManageContactsAction.UnSub,
      },
    ],
  };

  return mailjet
    .post("contact", { version: "v3" })
    .id(id)
    .action("managecontactslists")
    .request<ContactSubscription.PostContactManageContactsListsResponse>(
      contactLists
    );
};

The Action property in my contact list subscription body complains if I don't use the BulkContactManagement.ManageContactsAction enum, but if I try to compile and run this code, it fails because BulkContactManagement is not exported in the bundle from node-mailjet. So we get cannot read property "ManageContactsAction" of undefined.

@ai-wintermute ai-wintermute changed the title Cannot read property 'MessageState' of undefined [WIP] Cannot read property 'MessageState' of undefined Jul 14, 2023
@Arsalanliaqat
Copy link

Arsalanliaqat commented Jul 27, 2023

I'm just reaching out to ask about the status of this issue. This issue impacts our work, and Is there any update or approximate timeline for a fix?

TypeError: Cannot read properties of undefined (reading 'TrackOpens')
TypeError: Cannot read properties of undefined (reading 'TrackClicks')
TypeError: Cannot read properties of undefined (reading 'Response')

@ai-wintermute
Copy link
Collaborator

Hi @Arsalanliaqat PR with the hotfix is ready but it breaks the default import functionality. That's why we're considering other options, and it takes time. Sorry for the inconvenience.

@Jerome91410
Copy link

Hello,

Any news?

I'm facing same issue when I want to access to enum declared by SendEmail.
I had to self declare the value in my own code...
image

Could you export them (the enums) outside the namespace as a workaround/hotfix??

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants