【问题标题】:Node: Save generated Stripe "Customer ID" in variable节点:将生成的 Stripe“客户 ID”保存在变量中
【发布时间】: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


【解决方案1】:

方法一

在调用async 函数时,您应该再次在其前面添加await 以使其工作。我想这个函数链async可能会一直持续到所有东西都在async/await中;)

async function stripeCustomerCreation() {
    const response = await stripe.customers.create({
        email: req.body.email.toLowerCase(),
    })
    customerId = response.id
    return customerId
};

const customerId = await 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

stripe.customers.create({
    email: req.body.email.toLowerCase(),
}).then(response => {
    customerId = response.id
    console.log(customerId) // defined

    // 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
    });
});

对于你当前的代码,我们可以通过下面的代码来解决。

直接分配一个async 函数会给你一个解决的承诺。然后你必须像下面的代码一样resolve它。

async function stripeCustomerCreation() {
  const response = await stripe.customers.create({
    email: req.body.email.toLowerCase(),
  })
  customerId = response.id
  return customerId
};
stripeCustomerCreation()
  .then(customerId => {
    console.log(customerId) // Defined
  });

【讨论】:

  • 你的意思是在开头,像这样? "await router.post('/register', (req, res)"? :D
  • async router.post(。我觉得你在这里使用方法2会更好!
  • 感谢您的链接。但这有点奇怪,我更新了我的代码,所以现在有第三个代码块。
  • async/await 保持联系可能很奇怪!别担心!了解规则 0:要调用 await,它应该在 async 函数内。我建议你使用我现在写的方法2。
  • 这个!奇迹般有效。谢谢朋友,你帮了我。将您的答案标记为可接受。
【解决方案2】:

这是因为代码的异步行为。

// Statement 1
stripe.customers.create({
    email: req.body.email.toLowerCase(),
}).then(response => {
    customerId = response.id
    // customerId is defined here
}); 

// Statement 2
console.log(customerId)

上面的执行顺序是,

声明 2

声明 1

这是因为Statement 1 是一个数据库操作,比Statement 2 需要更多的时间来完成。所以Statement 2首先运行,此时变量response是未定义的。这就是你变得不确定的原因。

为了让您正确访问变量customerId,我们有几种方法:

方法一

如果你熟悉async/await

const response = await stripe.customers.create({
    email: req.body.email.toLowerCase(),
})
customerId = response.id

console.log(customerId) // defined

注意:为了让await 工作,应该在async 函数中调用它。


方法 2

stripe.customers.create({
    email: req.body.email.toLowerCase(),
}).then(response => {
    customerId = response.id
    console.log(customerId) // defined
    // Rest of your code here
});

注意:这可能会导致您遇到一种称为链式承诺的情况。尝试谷歌搜索!


您可以根据自己的方便选择任何一种方法使其工作。

希望这会有所帮助! :)

【讨论】:

  • 感谢您编辑并提供一些示例。我尝试了异步/等待方法。但它仍然是“未定义”,我要更新我的代码。
  • 尝试方法2。同样,在定义customerId之前添加console.log(JSON.stringify(response))。它可能会帮助你!
  • 但是方法2仍然只在函数本身定义了“customer.id”,你告诉我的?正如你所说,我必须等待回复。
猜你喜欢
  • 2020-05-21
  • 2011-08-31
  • 2021-06-07
  • 2022-08-10
  • 2014-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
相关资源
最近更新 更多