添加mailchimp订阅者错误:name.toUpperCase不是函数

时间:2017-11-24 15:20:54

标签: javascript mailchimp axios mailchimp-api-v3.0

我正在尝试将订阅者添加到我的mailchimp列表中,但不断收到错误TypeError: name.toUpperCase is not a function。这是我的代码,下面列出了完整输出。任何人都知道mailchimp是否有错误或者我做错了什么?

CODE

const addSubscriber = function addSubscriber(user) {
  console.log('IN ADD SUBSCRIBER');
  return axios({
    method: 'post',
    url: `https://${mailchimpDC}.api.mailchimp.com/3.0/lists/${mailchimpListId}/members`,
    data: {
      email_address: user.email,
      status: "subscribed",
      merge_fields: {
          FNAME: user.name.firstName,
          LNAME: user.name.lastName,
          ID: user._id,
      },
    },
    headers: `Authorization: apikey ${mailchimpAPIKey}`,
  })
  .then(res => {
    console.log('res', res);
  })
  .catch((err) => {
    console.log('err', err);
  })
};

输出

IN ADD SUBSCRIBER
err with mailchimp request [TypeError: name.toUpperCase is not a function]

1 个答案:

答案 0 :(得分:0)

终于明白了。授权标头格式不正确!授权应该是头对象下的一个关键。我还添加了Content-Type。现在一切正常!

const addSubscriber = function addSubscriber(user) {
  console.log('IN ADD SUBSCRIBER');
  return axios({
    method: 'post',
    url: `https://${mailchimpDC}.api.mailchimp.com/3.0/lists/${mailchimpListId}/members`,
    data: {
      email_address: user.email,
      status: "subscribed",
      merge_fields: {
          FNAME: user.name.firstName,
          LNAME: user.name.lastName,
          ID: user._id,
      },
    },
    headers: {
      'Content-Type': 'application/json',
      Authorization: `apikey ${mailchimpAPIKey}`,
    },
  })
  .then(res => {
    console.log('res', res);
  })
  .catch((err) => {
    console.log('err with mailchimp request', err);
  })
};