Node.js环境下如何通过Stripe API获取所有客户的邮箱地址
Stripe 全量客户邮箱获取实现方案
问题根因
你当前仅输出第一个客户邮箱,是因为data[0]只访问了客户数组的第一个下标。stripe.customers.list接口返回结果中的data字段就是存储所有客户对象的数组,遍历该数组即可获取全部邮箱。
实现方案
方案1:客户总量≤100时的极简实现
Stripe 列表接口默认单次返回最多10条数据,可通过limit参数调整为最大100条,适合客户量少的场景:
const customers = await stripe.customers.list({ limit: 100 // 单次请求允许返回的最大客户数量 }); // 批量提取所有邮箱 const allCustomerEmails = customers.data.map(customer => customer.email); // 输出结果 console.log(allCustomerEmails);
方案2:客户总量>100时的分页实现
如果客户总量超过100,需要处理分页拉取,可直接使用Stripe Node SDK内置的自动分页迭代器,避免手动处理分页游标:
const allCustomerEmails = []; // 自动遍历全部分页的客户数据 for await (const customer of stripe.customers.list({ limit: 100 }).autoPagingEach()) { // 可选:过滤掉未填写邮箱的无效数据 if (customer?.email) { allCustomerEmails.push(customer.email); } } // 输出全量邮箱数组 console.log(allCustomerEmails);
可选扩展
如果需要对邮箱去重,可使用Set处理:
const uniqueEmails = [...new Set(allCustomerEmails)];
内容的提问来源于stack exchange,提问作者gianlps
相关产品推荐
相关产品推荐

