【问题标题】:How to add a new user to Mailchimp campaign using cloud functions?如何使用云功能将新用户添加到 Mailchimp 活动?
【发布时间】:2020-05-21 19:49:14
【问题描述】:

我正在尝试将新用户添加到 Mailchimp 活动。我收到两个错误,第一个说:

Function returned undefined, expected Promise or value

第二个错误的日志是:

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.' } ] }

这是我的功能

   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);
      });
  });

【问题讨论】:

    标签: javascript node.js firebase google-cloud-functions mailchimp


    【解决方案1】:

    您的云函数中有两个错误,您已检测到:

    第一个是您必须在后台触发的 Cloud Function 中返回一个 Promise(或一个值),以向平台表明它已完成。请观看Firebase video series 中有关“JavaScript Promises”的三个视频,了解有关此关键点的更多详细信息。

    第二个问题是您对字段status 的值“不是有效选择”。看看 Mailchimp doc,如果我没记错的话,正确的值是:

    subscribed、pending、unsubscribed 或 cleaned。


    因此,以下更改应该可以解决问题:

       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;
          });
      });
    

    【讨论】:

      猜你喜欢
      • 2016-01-04
      • 2021-12-02
      • 2018-12-29
      • 2019-07-13
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 2015-12-19
      • 1970-01-01
      相关资源
      最近更新 更多