尝试在控制器中使用express时出错

时间:2017-09-25 10:42:00

标签: javascript angularjs node.js express stripe-payments

我一直在尝试实施paymentCtrl来处理Stripe付款,但我无法让快递工作。当我执行此代码时,我得到以下错误。我对此很新,并想了解为什么我会收到此错误。

错误:

  

未知提供商:appProvider< - app< - paymentCtrl

app.js:

angular.module('userApp', ['appRoutes', 'userControllers', 'userServices', 'ngAnimate', 'mainController', 'authServices', 'managementController', 'paymentController'])

.config(function($httpProvider) {
    $httpProvider.interceptors.push('AuthInterceptors');
});

payment.html:

<div>

<form action="/charge" method="post">
  <script
    src="https://checkout.stripe.com/checkout.js"
    class="stripe-button"
    data-key="pk_test_..."
    data-amount="3000"
    data-name="walla"
    data-description="this is not a dog"
    data-locale="auto"
    data-currency="gbp"
  ></script>


  </script>
</form>

</div>

paymentCtrl:

angular.module('paymentController', [])

.controller('paymentCtrl', function(app, passport) {

  app.post('/charge', function(req, res){

  });


});

server.js:

var express = require('express'); // ExperssJS Framework
var app = express(); // Invoke express to variable for use in application
var port = process.env.PORT || 8080; // Set default port or assign a port in enviornment
var morgan = require('morgan'); // Import Morgan Package
var mongoose = require('mongoose'); // HTTP request logger middleware for Node.js
var bodyParser = require('body-parser'); // Node.js body parsing middleware. Parses incoming request bodies in a middleware before your handlers, available under req.body.
var router = express.Router(); // Invoke the Express Router
var appRoutes = require('./app/routes/api')(router); // Import the application end points/API
var path = require('path'); // Import path module
var passport = require('passport'); // Express-compatible authentication middleware for Node.js.
var social = require('./app/passport/passport')(app, passport); // Import passport.js End Points/API
var stripe = require('stripe')('sk_test_...');

app.use(morgan('dev')); // Morgan Middleware
app.use(bodyParser.json()); // Body-parser middleware
app.use(bodyParser.urlencoded({ extended: true })); // For parsing application/x-www-form-urlencoded
app.use(express.static(__dirname + '/public')); // Allow front end to access public folder
app.use('/api', appRoutes); // Assign name to end points (e.g., '/api/management/', '/api/users' ,etc. )

//
// <---------- REPLACE WITH YOUR MONGOOSE CONFIGURATION ---------->
//
mongoose.connect('mongodb://localhost:27017/tutorial', function(err) {
  if (err) {
    console.log('Not connected to the database: ' + err);
  } else {
    console.log('Successfully connected to MongoDB');
  }
});

// Set Application Static Layout
app.get('*', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/app/views/index.html')); // Set index.html as layout
});

// Start Server
app.listen(port, function() {
    console.log('Running the server on port ' + port); // Listen on configured port
});

2 个答案:

答案 0 :(得分:0)

您的角度控制器应注入$http模块,在客户端代码中未定义应用程序,因为客户端不会从服务器继承代码。

.post模块包含.get .put .delete方法以及许多其他方法,例如angular.module('paymentController', []) .controller('paymentCtrl', function(app, passport, $http) { $http.post('/charge', {}, function(req, res){ }); }); 等。

{}

$ http.post(app.post('/charge', function(req, res) { // Your server endpoint for /charge POST action // req.body contains passed data. }); )中的第二个参数是您要传输到控制器的数据。

最后,您需要一个服务器方法来检索POST请求。

ng-controller

当您尝试使用Angular将数据发送到服务器时,您需要将表单数据绑定到模型,并删除表单或监听表单提交事件并以您希望的方式自行发出POST请求到。

由于您没有包含整个HTML文档,因此很难看出是否包含任何其他错误,例如文档中缺少minorTickInterval声明作为评论说明。

答案 1 :(得分:0)

我假设您关注this Stripe tutorial所以您需要做的就是:

您在 paymentCtrl 中编写的代码应该在app.listen之前复制到server.js

// Set Application Static Layout
app.get('*', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/app/views/index.html')); // Set index.html as layout
});

app.post('/charge', function(req, res){}); 


// Start Server
app.listen(port, function() {
    console.log('Running the server on port ' + port); // Listen on configured port
});
  

未知提供商:appProvider&lt; - app&lt; - paymentCtrl

这意味着您尝试注入的app变量在您的客户端代码中不存在。