Issue with Mailchimp API

Hi guys, I’ve followed the instructions on the forum on how to pass data from wix dataset (forms) to mailchimp api however I have an issue. What could be the solution here? Thx :slight_smile:

Here is the problem I currently have when I try to pass the request from wix preview:
Hook afterInsert for collection LeadGeneration result ignored! Expected hook result to resolve to an object with an ‘_id’ property, but got [Undefined]

Here is the page code:

import {sendToMailChimp} from ‘backend/mailChimp.jsw’;

export function LeadGeneration_afterInsert() {

var data = {
“email_address”:$w(“#input3”).value,
“merge_fields”: {
“FNAME”:$w(“#input1”).value,
“LNAME”:$w(“#input2”).value
//“PHONE”:$w(“#input3”).value
},
“status”:“subscribed”
};

data = JSON.stringify(data, ‘\t’);

sendToMailChimp(data); 
console.log("API Test"); 

}

Here the backend code:

// Filename: backend/mailChimp.jsw (web modules need to have a .jsw extension)

import {fetch} from ‘wix-fetch’;

const API_KEY_64 = “anystring:[key]”;
const LIST_ID = “[list]”;
export function sendToMailChimp(data) {
fetch(“https://[us].api.mailchimp.com/3.0/lists/” + LIST_ID + “/members”, {
method: ‘post’,
headers: {
‘Authorization’ : 'Basic ’ + API_KEY_64,
‘Content-Type’ : ‘application/json’
},
body: data
});
}

Have you checked your MailChimp list? Sometimes it works but the editor throws the above warning.

Your code looks fine. Although the base URL https://us20.api.mailchimp.com differs for each person. You should check your own MC dashboard if the url is same or not.

Another thing, hope this is not the actual API Key and LIST ID that you have posted. If it is then there is a problem with your API Key (and remove it from your post)

Hi, thx. I just realized I posted my api key removed.

I was checking in parallel with mailchimp list and there wasn’t any update there. Not sure what is the issue. I’ve checked the API calls on MC side and the list is empty. The base URL starts with us20 on my dashboard.

@vuuuk81 I have tested the following code and it works. I just added a console.log() and return to the frontend/backend so that the response from the api gets printed.

Backend:

//mailChimp.jsw

import {fetch} from 'wix-fetch';

const API_KEY_64 = "xxxxxxxxxxxxxx";
const LIST_ID = "xxxxxxxxxxxxxxxx";

export async function sendToMailChimp(data) {   
 const response = await fetch("https://us15.api.mailchimp.com/3.0/lists/" + LIST_ID + "/members", {
        method: 'post',
        headers: {
 'Authorization' : 'Basic ' + API_KEY_64,
 'Content-Type' : 'application/json'
        },
        body: data
    });
 return response.json();
}

Page Code:

async function perform() {
     var data = {
         "email_address": $w("#input3").value,
         "status": "subscribed",
         "merge_fields": {  
             "FNAME": $w("#input1").value,
             "LNAME": $w("#input2").value
           } 
     };
    data = await JSON.stringify(data, '\t');
    sendToMailChimp(data)
    .then( (response) => {
        console.log(response);
    });
}

This will allow you to see what error is being returned.

Hi @shan thanks for code and help! Much appreciated and will test this to see whats wrong.