[英]How to add a new user to Mailchimp campaign using cloud functions?
I am trying to add the new user to the Mailchimp campaign.我正在尝试将新用户添加到 Mailchimp 活动中。 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.第一个是您必须在后台触发的 Cloud Function 中返回一个 Promise(或一个值),以向平台表明它已完成。 Please watch the three videos about "JavaScript Promises" from the Firebase video series for more details on this key point.请观看Firebase 视频系列中关于“JavaScript Promises”的三个视频,以了解有关此关键点的更多详细信息。
The second problem is that your value for the field status
"is not a valid choice".第二个问题是您对字段status
值“不是一个有效的选择”。 Have a look at the Mailchimp doc , if I am not mistaking, the correct values are:看看 Mailchimp doc ,如果我没有记错的话,正确的值是:
subscribed
, pending
, unsubscribed
or cleaned
. subscribed
、 pending
、 unsubscribed
或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;
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.