【发布时间】:2019-07-12 08:03:00
【问题描述】:
当一个新用户在我的页面上注册时,除了用户名和电子邮件之外,我还想生成一个 Stripe 客户 ID 以将其保存在数据库中。到目前为止,一切都很好。正如我在 Stripe 测试仪表板中看到的那样,客户创建工作正常。
但是如何将 Stripe 响应(即客户 ID)保存在新变量中以供日后使用?
stripe.customers.create({
email: req.body.email.toLowerCase(),
}).then(response => {
customerId = response.id
});
console.log(customerId)
@bharadhwaj 等待/异步建议后的代码更新:
async function stripeCustomerCreation() {
const response = await stripe.customers.create({
email: req.body.email.toLowerCase(),
})
customerId = response.id
};
stripeCustomerCreation();
console.log(customerId)
// Create new user object and apply user input
let user = new User({
email: req.body.email.toLowerCase(),
username: req.body.username.toLowerCase(),
password: req.body.password
});
更新 2:控制台给了我一个 Promise { pending },但没有 customerId 再多一行。 :(
async function stripeCustomerCreation() {
const response = await stripe.customers.create({
email: req.body.email.toLowerCase(),
})
customerId = response.id
return customerId
};
var customerId = stripeCustomerCreation();
console.log(customerId)
【问题讨论】:
-
变量
response已经是一个JSON。无需再次致电reponse.json()! -
是的,谢谢,解决了这个问题。但是为什么customerID仍然未定义?
-
Create 是一个异步函数。在读取变量之前,您不会等待完成它
-
@dmnktoe 添加为答案!检查它是否解决了您的问题。
标签: javascript node.js express server stripe-payments