我正在使用NodeJS服务器的网站上工作。
我实施了Stripe API,以使人们每月付款。
我需要在数据库中获取他们的电子邮件/ custumer_ID(以及其他信息),但是我无法设法获得这些信息。
这是我的代码:
app.post("/charge", (req, res) => {
stripe.customers.create({
email: req.body.stripeEmail,
source: req.body.stripeToken
})
.then(customer =>
stripe.subscriptions.create({
plan: 'plan_EQoygL3d1jnHM2',
customer: customer.id
}))
.then(charge => res.render("charge.pug"));
var sql = "INSERT INTO authentification (customerID, email, discord_key, datePayement) values ?";
var datenow = new Date();
var values = [
[customer.id, email, generateKey(), datenow]
];
DB.query(sql, values, function (err, result) {
if (err) throw err;
console.log(result);
});
});
这是我得到的错误:
ReferenceError: customer is not defined.
Customer not defined on this part :
var values = [
[customer.id, email, generateKey(), datenow]
];
我还想知道我的操作方式是否安全,或者是否还有其他方法可以执行?
非常感谢您的帮助!
我是节点JS的新手。
答案 0 :(得分:2)
customer变量仅存在于此函数的范围内
customer => stripe.subscriptions.create({
plan: 'plan_EQoygL3d1jnHM2',
customer: customer.id
})
这是写作的简写形式
function(customer) {
stripe.subscriptions.create({
plan: 'plan_EQoygL3d1jnHM2',
customer: customer.id
})
}
,当stripe.customers.create完成运行时被调用。它是异步的,我将不对其进行详细介绍,但这只是意味着它不会阻塞执行线程,而是全部移至下一行代码,并在每次调用时调用上述函数。 Stripe API会回复。
考虑到这一点,这意味着现在发生的是
var values = [
[customer.id, email, generateKey(), datenow]
];
必须沿着 ReferenceError:未定义客户
抛出错误您可以通过多种选择来解决此问题。
最容易理解和阅读的是,前提是您使用的节点版本高于7.6(在terminal / cmd中键入node -v
),并使用async / await处理异步调用,因此< / p>
app.post("/charge", async (req, res) => {
try {
var customer = await stripe.customers.create({
email: req.body.stripeEmail,
source: req.body.stripeToken
})
await stripe.subscriptions.create({ // no point in awaiting here
plan: 'plan_EQoygL3d1jnHM2',
customer: customer.id
}))
res.render("charge.pug")
var sql = "INSERT INTO authentification (customerID, email, discord_key, datePayement) values ?";
var datenow = new Date();
var values = [
[customer.id, customer.email, generateKey(), datenow]
];
DB.query(sql, values, function (err, result) {
if (err) throw err;
console.log(result);
});
} catch (ex) {
console.error('/charge encountered exception', exception) // the try/catch block is optional, but should help you figure out further problems along the way
res.sendStatus(503)
}
});
但是,如果您受限于较低版本的Node,则可以像这样继续使用Promises(简而言之,您看到的.then
模式)
app.post("/charge", (req, res) => {
stripe.customers.create({
email: req.body.stripeEmail,
source: req.body.stripeToken
}).then(customer => {
var sql = "INSERT INTO authentification (customerID, email, discord_key, datePayement) values ?";
var datenow = new Date();
var values = [
[customer.id, customer.email, generateKey(), datenow]
];
DB.query(sql, values, function (err, result) {
if (err) throw err;
console.log(result);
});
return stripe.subscriptions.create({ // returning a Promise here means the next .then will wait for it to solve, before rendering 'charge.pug'
plan: 'plan_EQoygL3d1jnHM2',
customer: customer.id
})
})
.then(charge => res.render("charge.pug"));
.catch(exception => {
console.error('/charge encountered exception', exception) // the .catch is optional, but should help you figure out further problems along the way
res.sendStatus(503)
})
});
希望这会有所帮助!