简体   繁体   English

如何使用云功能向 Mailchimp 活动添加新用户?

[英]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 . subscribedpendingunsubscribedcleaned


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.

相关问题 如何检测在云功能中创建的新 firebase 用户? - How to detect a new firebase user created in cloud functions? 使用Cloud Functions添加子级? - Add child using Cloud Functions? 如何使用云功能将新数据推送到Firebase实时数据库中? - How to push new data into firebase realtime database using cloud functions? 如何在MailChimp API v3.0中为广告系列分配模板? - How to assign a template to a campaign in MailChimp API v3.0? 如何使用云功能在 Firestore 中添加虚拟数据 - How to add Dummy Data in Firestore using cloud functions 如何在CRM中使用JavaScript将营销列表添加到广告系列 - How to add marketing List to campaign using javascript in crm 如何使用云功能通过webhook将Dialogflow中的用户响应保存到google云端防火墙 - How to save the user response in Dialogflow via webhook to google cloud firestore using cloud functions 使用云函数向文档添加字段 - Add field to document using cloud functions 使用Firestore获取/添加Cloud中的文档功能 - Get/add Document in Cloud functions using Firestore 当用户 (A) 使用 firebase 云功能和 Flutter 关注用户 (B) 时如何接收通知 - How to receive a notification when a user (A) )follows user (B) using firebase cloud functions and Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM