通过 API 获取和存储 MailChimp 兴趣的 Typescript 方法
Posted
技术标签:
【中文标题】通过 API 获取和存储 MailChimp 兴趣的 Typescript 方法【英文标题】:Typescript method to get and store MailChimp interests via the API 【发布时间】:2021-09-19 11:18:34 【问题描述】:我正在尝试通过 Audience ID 查询 API 来获取我对 Mailchimp 的兴趣;我创建了下面的代码,它可以正确检索所有内容,但是当我在forEach
循环之外显示检索到的数据时,我没有存储任何内容。
let obj = ;
let arrays = [];
let newdata;
let textlist;
try
const data = await mailchimp.lists.getListInterestCategories(MailChimpAudienceId);
//console.log(data);
const categories = data.categories;
if (!categories.length)
console.log(`No categories found in Mail chimp list $MailChimpAudienceId`);
categories.forEach(async element =>
//console.log(element.id);
obj[element.title] = element.id;
arrays.push(element.id);
newdata = await mailchimp.lists.listInterestCategoryInterests(
MailChimpAudienceId,
element.id,
);
const interests = newdata.interests;
interests.forEach(async element =>
//console.log(element.id, element.name);
obj[element.name] = element.id;
arrays.push(element.id);
textlist += "," + element.id;
);
// console.log(interests);
console.log("obj0>>", obj);
console.log("arr0>>", arrays);
console.log("txt0>>", textlist);
);
console.log("obj1>>", obj);
console.log("arr1>>", arrays);
console.log("txt1>>", textlist);
catch (e)
console.log("err>> ", e);
console.log("obj2>>", obj);
console.log("arr2>>", arrays);
console.log("txt2>>", textlist);
输出:
obj1>> MickeyMouse: '21c3bbb614', Music: 'ac86c9d94b'
arr1>> [ '21c3bbb614', 'ac86c9d94b' ]
txt1>> undefined
obj2>> MickeyMouse: '21c3bbb614', Music: 'ac86c9d94b'
arr2>> [ '21c3bbb614', 'ac86c9d94b' ]
txt2>> undefined
obj0>>
MickeyMouse: '21c3bbb614',
Music: 'ac86c9d94b',
Guitar: '9e05119885',
Drums: 'ca5b24595d',
Bass: '0ce6ed21da'
arr0>> [
'21c3bbb614',
'ac86c9d94b',
'9e05119885',
'ca5b24595d',
'0ce6ed21da'
]
txt0>> undefined,9e05119885,ca5b24595d,0ce6ed21da
obj0>>
MickeyMouse: '21c3bbb614',
Music: 'ac86c9d94b',
Guitar: '9e05119885',
Drums: 'ca5b24595d',
Bass: '0ce6ed21da',
'Big Ears': '5154e49101',
News: 'e49c281f9f',
Fundraising: 'aad8e5ba96'
arr0>> [
'21c3bbb614',
'ac86c9d94b',
'9e05119885',
'ca5b24595d',
'0ce6ed21da',
'5154e49101',
'e49c281f9f',
'aad8e5ba96'
]
txt0>> undefined,9e05119885,ca5b24595d,0ce6ed21da,5154e49101,e49c281f9f,aad8e5ba96
如何存储这些值,以便数组、对象或文本在二级循环之外具有值?
【问题讨论】:
【参考方案1】:看起来我的问题与异步方面有关,如以下答案所示:Resolve await when message arrives
对于那些在家玩的人,答案是添加类似这样的暂停:
setTimeout(_ =>
// If we have categories, then display them here
console.log(
`Audience ID $MailChimpAudienceId has the interests: $arrays`,
);
, 1000); // display after a delay
【讨论】:
以上是关于通过 API 获取和存储 MailChimp 兴趣的 Typescript 方法的主要内容,如果未能解决你的问题,请参考以下文章
Mailchimp API:如何获取列表中选择特定兴趣的成员
如何使用 Mailchimp API 3.0 从用户或兴趣中获取超过 10 个条目?
是否可以在 mailchimp API 3.0 中按成员获取细分?