2014-10-30 69 views
0

我只是試圖向Stripe發送數據來處理客戶。但是請求沒有定義?這怎麼可能?未在Nodejs應用程序中定義的請求

錯誤:

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: '[email protected]' 
    }, 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  
}); 

回答

1

var stripeToken = req.body.stripeToken;是不是在正確的範圍,你有它的外請求處理程序。

此外,您沒有將任何參數傳遞給您的subscribeUser(),並且您試圖多次響應相同的請求。我的猜測是你想要的東西是這樣的:

function subscribeUser(token, res){ 
    stripe.customers.create({ 
     card: token, 
     plan: 'standard', 
     email: '[email protected]' 
    }, 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  
}); 
0

req不是整個文件的範圍內聲明。唯一聲明的地方是參數列表function (req, res),並且只在函數定義中創建一個變量作用域。你可能意味着改變下一行這樣:

subscribeUser(req.body.stripeToken); 

,然後換另一函數定義如下:

function subscribeUser(stripeToken){ ...