我只是想向Stripe发送数据来处理客户。但请求没有定义?这怎么可能?
错误:
13:52:17 web.1 | /Users/admin/herokutest/app/routes.js:139
13:52:17 web.1 | var stripeToken = req.body.stripeToken;
13:52:17 web.1 | ReferenceError:未定义req
App.js文件:
var stripeToken = req.body.stripeToken;
function subscribeUser(token, res){
stripe.customers.create({
card: stripeToken,
plan: 'standard',
email: 'test@test.test'
}, function(err, customer) {
if (err) {
res.send({ok: false, message: 'Uh oh. there was a problem processing your card (error: ' + JSON.stringify(err) + ')'});
} else {
res.send({ok: true, message: 'Perfect, you have been subscribed to a plan'}),
res.render('subscribe', { title: 'Congratulations!' })
}
});
}
app.post('/getstarted3', function (req, res) {
subscribeUser();
res.render('subscribe', { title: 'Welcome' }); // load the index.jade file
});
答案 0 :(得分:1)
行var stripeToken = req.body.stripeToken;
的范围不正确,您在请求处理程序之外。
此外,您没有向subscribeUser()
传递任何参数,并且您尝试多次响应同一请求。我的猜测是你想要这样的东西:
function subscribeUser(token, res){
stripe.customers.create({
card: token,
plan: 'standard',
email: 'test@test.test'
}, function(err, customer) {
if (err) {
res.send({ok: false, message: 'Uh oh. there was a problem processing your card (error: ' + JSON.stringify(err) + ')'});
} else {
res.send({ok: true, message: 'Perfect, you have been subscribed to a plan'}),
// you can't respond to a request twice, it's one or the other ...
//res.render('subscribe', { title: 'Congratulations!' })
}
});
}
app.post('/getstarted3', function (req, res) {
subscribeUser(req.body.stripeToken, res);
// you can't respond multiple times to the same request
//res.render('subscribe', { title: 'Welcome' }); // load the index.jade file
});
答案 1 :(得分:0)
req
未在整个文件的范围内声明。它声明的唯一位置是在参数列表function (req, res)
中,并且只在函数定义中创建一个作用域的变量。您可能打算将下一行更改为:
subscribeUser(req.body.stripeToken);
然后将其他函数定义更改为:
function subscribeUser(stripeToken){ ...