MailChimp - My first module... Its working :) Just need a little help returning the response

MailChimp - My first module… Its working :slight_smile: Just need a little help returning the response…
this is the part i can figure out… as I need to get a response…
It adds to user and user gets confirmation mail… but how do I get a “response” to show the user if they added or now… i have tried a few things… but just not getting it right…

I looked at this.

I used the npm

to compile this simple MailChimp module.
Any help would be appreciated please.

So it working if the email not in the list… see via the terminal…
ffffffffffffffff

So i just need help with the “if no response” then output something…

Here is the files.
mailchimaddmembertolist.zip (1.2 KB)

Module

[
{
    type: 'mailchimp_create_mailchimp_add_member_to_list',
    module : 'mailchimaddmembertolist',
    action : 'create_event_file',
    groupTitle : 'MailChimp',
    groupIcon : 'fas fa-envelope',
    title : 'MailChimp - Add Member to List @@var(actionName)@@',
    icon : 'fas fa-envelope',
    dataPickObject: true,
    dataScheme: [
        {name: 'response', type: 'text'}
    ] ,
    properties : [
        {
        group: 'MailChimp',
        variables: [
            { 
                name: 'actionName', 
                optionName: 'name', 
                title: 'Name', 
                type: 'text', 
                required: true, 
                defaultValue: ''
            },
            { 
                name: 'output', 
                optionName: 'output', 
                title: 'Output', 
                type: 'boolean', 
                defaultValue: false 
            },
             { 
                 name: 'apikey', 
                 optionName: 'apikey', 
                 title: 'API Key',  
                 type: 'text', 
                 required: true, 
                 defaultValue: '', 
                 serverDataBindings: true
                },
            { 
                name: 'server', 
                optionName: 'server', 
                title: 'Server Prefix',  
                type: 'text', 
                required: true, 
                defaultValue: '', 
                serverDataBindings: true
            },
			{ 
                name: 'list_id', 
                optionName: 'list_id', 
                title: 'List ID',  
                type: 'text', 
                required: true, 
                defaultValue: '', 
                serverDataBindings: true
            },
			{ 
                name: 'email_address', 
                optionName: 'email_address', 
                title: 'Email Address',  
                type: 'text', 
                required: true, 
                defaultValue: '', 
                serverDataBindings: true
            }
        ]
        }
    ]
}]

and the the js
// JavaScript Document

 exports.create_event_file = async function (options) {
//https://www.npmjs.com/package/@mailchimp/mailchimp_marketing
//npm install @mailchimp/mailchimp_marketing

//the mailchimp object
const mailchimp = require('@mailchimp/mailchimp_marketing');  

// get the remaining dynamic variables
let apikey = this.parseRequired(options.apikey, 'string', 'parameter apikey is required.');
let server = this.parseRequired(options.server, 'string', 'parameter server is required.');
let list_id = this.parseRequired(options.list_id, 'string', 'parameter list_id is required.');
let email_address = this.parseRequired(options.email_address, 'string', 'parameter list_id is required.');

mailchimp.setConfig({
apiKey: apikey,
server: server,
});

const run = async () => {
  const response = await mailchimp.lists.addListMember(list_id, {
    email_address: email_address,
    status: "pending",
  });
  
  // console.log(response);
};

run();
	 };
1 Like

For the clever guys… how would i return the console.log as a variable after it initialized?
Im really not up to scratch with java script.

What i want to achieve it to output a var … return a var of the response.

const run = async () => {
    try {
        const response = await mailchimp.lists.addListMember(list_id, {
            email_address: email_address,
            status: "pending",
        })
        console.log(`Successfully added. ID is ${response.id}.`)
    }
    catch (e) {
        console.log(e.status);
        
    }
};

run();

@Mozzi

This should would just fine, bu I didn’t test it.

exports.create_event_file = async function (options) {


const mailchimp = require('@mailchimp/mailchimp_marketing');  

// get the remaining dynamic variables
let apikey = this.parseRequired(options.apikey, 'string', 'parameter apikey is required.');
let server = this.parseRequired(options.server, 'string', 'parameter server is required.');
let list_id = this.parseRequired(options.list_id, 'string', 'parameter list_id is required.');
let email_address = this.parseRequired(options.email_address, 'string', 'parameter list_id is required.');

mailchimp.setConfig({
apiKey: apikey,
server: server,
});

const response = await mailchimp.lists.addListMember(list_id, {
    email_address: email_address,
    status: "pending",
});

console.log('result: ',JSON.stringify(response));
return JSON.stringify(response);

};

I haven’t tested it though.

This is what my js looks like (but I’m returning an object)

exports.query = async function(options, name) {
    let o = await {
        'result':'success',
        'message':'',
        'data': {
            'fname': name,
            'surl': this.parse(options.server_url),
            'gql': this.parse(options.graphql),
            'gqlv': this.parse(options.gql_vars),
            'outut': this.parse(options.output)
        }
    };
    console.error('query',o);
    return o;
};

thanks… ill have a go… :slight_smile: might just put this up for paid work… as i cannot be bothered anymore to figure it out… :slight_smile: :slight_smile:

1 Like

email me at lance@spearstone.partners if you want it done and expanded for you.

1 Like

Try this:

var response = null;

try {
    response = await mailchimp.lists.addListMember(list_id, {
        email_address: email_address,
        status: "pending",
    });
    console.log(`Successfully added. ID is ${response.id}.`);
}
catch (e) {
    console.log(e.status);
}

return response;

I am assuming here that response from addListMember function is something that Wappler can understand - somthing that can be converted to JSON.
If that is not the case, you will need to modify the response variable to ensure it is an object or an array.

1 Like

A HUGE BIG THANKS to @JonL for going out of his way to help me with this!!!
He redid it all for me! And by that I have learned one hell of a lot on how to construct a hjson file… as mine was crap… and did not do the job… Hence I always as for “samples” from guys… as it easier to understand once you see how its constructed…

SO ALL I CAN SAY IS THANK YOU TO JONL…!!!

3 Likes

Would you mind sharing it?

1 Like

Congrats @Mozzi!

I’d love to see a copy too if you are willing to share…

I’m also curious to know if there was an advantage in using a module instead of just making some API calls to Mailchimp in a server action…

(I’ve not worked with Mailchimp so I know nothing yet about their API)

You’re welcome buddy. Thanks for the mention!

I had a look at the rest of the JS api and it all looks pretty straight forward so you should be able to create additional actions by copy&pasting 95% of the code you already have.

@mozzi as you already have noticed I am very picky about who I help in this community and probably very harsh with others. This is by design. When I got my first job in a software company I was clueless about everything and I remember asking for help to my team lead about how to build something. He told me “I will help you when you start hitting your head against the wall in desperation”. One of the best lessons that has been given to me.

With my help you just learnt 1% of what you needed. The other 99% is your doing and with that 99% you learnt also what doesn’t work. That will give you a huge head start for the next development.

Others just want to learn 1% and have everything else given. Those I refuse to help because in the end they will prove themselves as helpless.

Congrats on your first Nodejs module :slight_smile:

4 Likes

Hi @JonL thanks … but im not going to take any credit for this… lol…
As you did the complete thing…
Thank you for doing “your” first module :slight_smile: :slight_smile:

Not mine to share. If @JonL wants to great… but i cannot share something i did not do. So i cannot just assume I can. I have just made a post about a market place… so lets see.

Hi.
Anything you can rather do directly via the GUI of Wappler with server connect is so much easier in my opinion.

See my post about the share… but not mine to share, as i did not do the “final” version and would not be right just to assume i can.

I have also tagged you in a market place post and would like your opinion about it.