async-labs / saas

Build your own SaaS business with SaaS boilerplate. Productive stack: React, Material-UI, Next, MobX, WebSockets, Express, Node, Mongoose, MongoDB. Written with TypeScript.
https://saas-app.async-await.com
MIT License
4.11k stars 682 forks source link

LIST_IDS in mailchimp.ts #175

Closed satishkn closed 2 years ago

satishkn commented 2 years ago

This is in regards to the code of mailchimp.ts at the end of chapter 6.

I have followed the instructions and have tested the code. Even after I have defined the env variables properly, the LIST_IDS is empty (as in : {}) - not sure why.

However, after moving the the following lines: const LIST_IDS = { signups: process.env.MAILCHIMP_SAAS_ALL_LIST_ID, };

within the function addToMailchimp() it works. Would appreciate if you can look into it and help me understand why it would not work (before moving to the function).

Thanks,

klyburke commented 2 years ago

Hi @satishkn, thanks for reporting. We will look into this. In the meantime, could you please create a public repo with the exact code you are running? That will help us with testing.

satishkn commented 2 years ago

Here it is. https://github.com/satishkn/async-saas-mailchimp-issue

It is basically the same code as what you have in your repo (saas/book) except for the file / lines I had mentioned. Let me know if you need anything else.

Thx

tima101 commented 2 years ago

@satishkn @klyburke I guess it depends on how you print LIST_IDS value. You gotta add console.log in the right location plus you have call addToMailchimp method. This is because code inside mailchimp.ts does not get mounted on Express's server so no value will get printed from mailchimp.ts if you just start server.

Currently, this code (https://github.com/async-labs/saas/blob/master/saas/api/server/mailchimp.ts) is deployed to our saas demo and we get >100 emails added to Mailchimp every week. So it should work as expected.

Hope this helps. We will test later today anyway.

klyburke commented 2 years ago

@satishkn thanks again for reporting. I tested more, and you are correct that LIST_IDS is undefined with the way mailchimp.ts is currently written.

To fix this problem, you can add the following 2 lines below import at the top of mailchimp.ts:

import fetch, { Response } from 'node-fetch';

// eslint-disable-next-line
require('dotenv').config();

The problem is that code from mailchimp.ts does not get imported to server.ts, so you need to use dotenv at the top of the file to access the environmental variables define with process.env.

As a comparison, code from google-auth.ts does not require dotenv because google-auth.ts is indeed imported to server.ts, which has dotenv.


@satishkn @tima101 Our saas demo that we deployed to Heroku does not throw this error, probably because of how Heroku populates env vars inside built project.

However, the error comes up when running web app locally. I will update code and content later today. Thanks!


Update: fixed the code and updated text in the book. All set now.

satishkn commented 2 years ago

Thanks for looking into it and fixing it too.