I am trying to add the new user to the Mailchimp campaign. I get two errors the first one says:
Function returned undefined, expected Promise or value
Second error's log is :
Error: The resource submitted could not be validated. For field-specific details, see the 'errors' array.
type: 'http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/',
title: 'Invalid Resource',
status: 400,
detail: 'The resource submitted could not be validated. For field-specific details, see the \'errors\' array.',
instance: '549e8553-5ef1-4600-9271-07255f4673fe',
errors:
[ { field: 'status',
message: 'The value you selected is not a valid choice.' } ] }
This is my function
exports.userCreated = functions.auth.user().onCreate((user) => {
const email = user.email;
// const displayName = user.name;
// make Mailchimp API request to add a user
Mailchimp
.post('/lists/id/members', {
email_address: email,
status: 'Signed Up',
// optional: requires additional setup
merge_fields: {
"EMAIL" : email
}
})
.then(function(results) {
console.log('Successfully added new Firebase user', email, 'to Mailchimp list',results);
})
.catch(function(err) {
console.log(email);
console.log('Mailchimp: Error while attempting to add registered subscriber —', err);
});
});
There are two errors in your Cloud Function, as you have detected:
The first one is that you MUST return a Promise (or a value) in your background triggered Cloud Function, to indicate to the platform that it has completed. Please watch the three videos about "JavaScript Promises" from the Firebase video series for more details on this key point.
The second problem is that your value for the field status
"is not a valid choice". Have a look at the Mailchimp doc, if I am not mistaking, the correct values are:
subscribed
, pending
, unsubscribed
or cleaned
.
So, the following changes should do the trick:
exports.userCreated = functions.auth.user().onCreate((user) => {
const email = user.email;
// const displayName = user.name;
// make Mailchimp API request to add user
return mailchimp
.post('/lists/id/members', {
email_address: email,
status: 'subscribed', // For example, it's up to you to find the correct status
// optional: requires additional setup
merge_fields: {
"EMAIL" : email
}
})
.then(function(results) {
console.log('Successfully added new Firebase user', email, 'to Mailchimp list',results);
return null;
})
.catch(function(err) {
console.log(email);
console.log('Mailchimp: Error while attempting to add registered subscriber —', err);
return null;
});
});