如何使用 javascript 中的两个请求与 mailchimp 连接两个数组?
How to concatenate two array by using two request in javascript with mailchimp?
标题可能不是很清楚。我要解释我的问题。
这是我的代码:
mailchimpMarketing = require("@mailchimp/mailchimp_marketing");
mailchimpMarketing.setConfig({
apiKey: "MY API KEY",
server: "MY SERVER",
});
const getEmailMembersFromList = async (listID, offset , count) => {
const response = await mailchimpMarketing.lists.getListMembersInfo(listID, {offset, count});
emails = response.members.map(member => {
return member.email_address
})
console.log(emails)
}
getEmailMembersFromList("MY LIST ID", 0, 1000)
getEmailMembersFromList("MY LIST ID", 1000, 58)
它在终端中显示两个电子邮件地址数组。我必须打两次电话,因为有1000多个电子邮件地址(总共1058个),最多1000个。
现在我想连接这两个数组,也许可以使用 .concat()
方法,但我不知道在我的情况下该怎么做。
编辑 1
mailchimpMarketing = require("@mailchimp/mailchimp_marketing");
mailchimpMarketing.setConfig({
apiKey: "MY API KEY",
server: "MY SERVER",
});
const getEmailMembersFromList = async (listID, offset , count) => {
const response = await mailchimpMarketing.lists.getListMembersInfo(listID, {offset, count});
emails = response.members.map(member => {
return member.email_address
})
//console.log(emails)
return emails
}
getEmailMembersFromList("MY LIST ID", 0, 1000)
getEmailMembersFromList("MY LIST ID", 1000, 58)
由于 async
始终发挥作用 return 您可以将所有请求添加到数组中并使用 Promise.all() 获取所有数据的承诺。
仅供参考:您没有从 getEmailMembersFromList
函数中 return 获取使其正常工作所必需的任何内容。
示例:
const firstGroup = getEmailMembersFromList("MY LIST ID", 0, 1000);
const secondGroup = getEmailMembersFromList("MY LIST ID", 1000, 58);
Promise.all([firstGroup, secondGroup]).then((values) => {
console.log(values);
});
试试这个代码:
let result = [];
const getEmailMembersFromList = async (listID, offset, count) => {
await mailchimpMarketing.lists
.getListMembersInfo(listID, { offset, count })
.then((response) => {
emails = response.members.map((member) => {
return member.email_address;
});
// try using spread operator, hopefully it should work
result = [...result, ...emails];
});
};
getEmailMembersFromList("MY LIST ID", 0, 1000);
getEmailMembersFromList("MY LIST ID", 1000, 58);
console.log(result);
标题可能不是很清楚。我要解释我的问题。
这是我的代码:
mailchimpMarketing = require("@mailchimp/mailchimp_marketing");
mailchimpMarketing.setConfig({
apiKey: "MY API KEY",
server: "MY SERVER",
});
const getEmailMembersFromList = async (listID, offset , count) => {
const response = await mailchimpMarketing.lists.getListMembersInfo(listID, {offset, count});
emails = response.members.map(member => {
return member.email_address
})
console.log(emails)
}
getEmailMembersFromList("MY LIST ID", 0, 1000)
getEmailMembersFromList("MY LIST ID", 1000, 58)
它在终端中显示两个电子邮件地址数组。我必须打两次电话,因为有1000多个电子邮件地址(总共1058个),最多1000个。
现在我想连接这两个数组,也许可以使用 .concat()
方法,但我不知道在我的情况下该怎么做。
编辑 1
mailchimpMarketing = require("@mailchimp/mailchimp_marketing");
mailchimpMarketing.setConfig({
apiKey: "MY API KEY",
server: "MY SERVER",
});
const getEmailMembersFromList = async (listID, offset , count) => {
const response = await mailchimpMarketing.lists.getListMembersInfo(listID, {offset, count});
emails = response.members.map(member => {
return member.email_address
})
//console.log(emails)
return emails
}
getEmailMembersFromList("MY LIST ID", 0, 1000)
getEmailMembersFromList("MY LIST ID", 1000, 58)
由于 async
始终发挥作用 return 您可以将所有请求添加到数组中并使用 Promise.all() 获取所有数据的承诺。
仅供参考:您没有从 getEmailMembersFromList
函数中 return 获取使其正常工作所必需的任何内容。
示例:
const firstGroup = getEmailMembersFromList("MY LIST ID", 0, 1000);
const secondGroup = getEmailMembersFromList("MY LIST ID", 1000, 58);
Promise.all([firstGroup, secondGroup]).then((values) => {
console.log(values);
});
试试这个代码:
let result = [];
const getEmailMembersFromList = async (listID, offset, count) => {
await mailchimpMarketing.lists
.getListMembersInfo(listID, { offset, count })
.then((response) => {
emails = response.members.map((member) => {
return member.email_address;
});
// try using spread operator, hopefully it should work
result = [...result, ...emails];
});
};
getEmailMembersFromList("MY LIST ID", 0, 1000);
getEmailMembersFromList("MY LIST ID", 1000, 58);
console.log(result);