gpt4 book ai didi

node.js - Nodejs 应用程序中未定义请求

转载 作者:太空宇宙 更新时间:2023-11-04 01:01:12 25 4
gpt4 key购买 nike

我只是想将数据发送到 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:未定义请求

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

最佳答案

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

关于node.js - Nodejs 应用程序中未定义请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26663811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com