通过 API 获取和存储 MailChimp 兴趣的 Typescript 方法
Typescript method to get and store MailChimp interests via the API
我正在尝试通过受众 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
我如何存储这些值,以便数组、对象或文本具有辅助循环之外的值?
看来我的问题出在异步方面,如以下答案所示:
对于那些在家里一起玩的人,答案是添加一个类似于这样的超时:
setTimeout(_ => {
// If we have categories, then display them here
console.log(
`Audience ID ${MailChimpAudienceId} has the interests: ${arrays}`,
);
}, 1000); // display after a delay
我正在尝试通过受众 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
我如何存储这些值,以便数组、对象或文本具有辅助循环之外的值?
看来我的问题出在异步方面,如以下答案所示:
对于那些在家里一起玩的人,答案是添加一个类似于这样的超时:
setTimeout(_ => {
// If we have categories, then display them here
console.log(
`Audience ID ${MailChimpAudienceId} has the interests: ${arrays}`,
);
}, 1000); // display after a delay