The Community will be temporarily unavailable starting on Friday February 28. We’ll be back as soon as we can! To learn more, check out our Announcements blog post.
Dec 29, 2022 11:04 AM - edited Dec 29, 2022 11:06 AM
let inputConfig = input.config();
//selecting preferred email for mailerlite
if (inputConfig.other_email != null){
inputConfig.preferred_email = inputConfig.other_email;
}
//constructing record for mailerlite
let content = {
"email":inputConfig.columbia_email,
"name":inputConfig.name,
"groups": ["1",]
};
//setting up mailerlite API call
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Accept", "application/json");
myHeaders.append("Authorization", "Bearer {$api_key}");
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: JSON.stringify(content),
redirect: 'follow'
};
fetch("https://connect.mailerlite.com/api/subscribers", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
When I test this locally, I manually set
let inputConfig = {
"preferred_email" : "test@test.com",
"other_email": null,
"name": "test dummy"
}
This works fine! I get the correct response,
{"data":{"id":"...","email":"test@test.com","status":"active","source":"api","sent":0,"opens_count":0,"clicks_count":0,"open_rate":0,"click_rate":0,"ip_address":null,"subscribed_at":"2022-12-29 17:50:36","unsubscribed_at":null,"created_at":"2022-12-29 17:50:35","updated_at":"2022-12-29 17:50:36","fields":{"name":null,"last_name":null,"company":null,"country":null,"city":null,"phone":null,"state":null,"z_i_p":null,"school":null,"class_year":null,"subtype":null},"groups":[{"id":"1","name":"Main Listserv","active_count":255,"sent_count":2487,"opens_count":1311,"open_rate":{"float":0.52714113389626,"string":"52.71%"},"clicks_count":151,"click_rate":{"float":0.060715721753116,"string":"6.07%"},"unsubscribed_count":8,"unconfirmed_count":0,"bounced_count":7,"junk_count":0,"created_at":"2022-03-30 09:33:40"}],"location":null,"opted_in_at":null,"optin_ip":null}}
However, when I run the script inside the script editor in the automations center in Airtable, I get a TypeError back from the MailerLite API. Why might this be? I thought it could be an issue with the input.config and the way I am accessing it, but I am not sure. Maybe another pair of eyes can help?
Thanks!
Dec 29, 2022 11:39 AM
@yva2002 Couple of questions that might suggest. Don't you forget to put variables on the left pane of your script editor? Is the response correct not showing "name": "test dummy"?
Dec 29, 2022 11:44 AM
Yes, I have selected the variables on the left pane. When I console.log(inputConfig) I get
{name: "Testing Dummy ", preferred_email: "test@test.com", other_email: null}
I console.log(content) I get
{email: "test@test.com", name: "Testing Dummy ", groups: Array(1)}
as desired.
So I can assume that the request being sent, as far as I can tell, seems correct? Except that it must not be, because I get an error from Mailerlite API as the response
Error {name: "TypeError"}
Dec 29, 2022 11:49 AM
Here's the actual console window for editing the script (I have a different email here than test@test.com, this is an email I can access the inbox of for testing reasons) - I noticed that it does give error highlighting for some attributed of inputConfig, but that seems to be because I previously had different names for variables inside inputConfig which haven't changed for the internal debugger
Dec 29, 2022 12:38 PM
It seems not ok when you try to assign a value to the input variable "preffered_email". You can use variables only for data input. If you need to assign a value to a cell you should use UpdateRecordAsync function.
Dec 30, 2022 06:05 AM
Update, it wasn't at all related to airtable or input config - the redirect request was causing the problem. Thanks for all your replies!