无法POST / api节点

时间:2015-07-08 16:39:17

标签: angularjs node.js mongodb express

我无法将AngularJs表单的响应发布到我的Mongo DB数据库。以下是

的代码段

控制器

   angular.module('myapp')
      .controller('AddCtrl', function($scope, $alert, Event) {
        $scope.addEvent = function() {
          Event.save({ eventName: $scope.eventName }).$promise
            .then(function() {
              $scope.eventName = '';
              $scope.addForm.$setPristine();
              $alert({
                content: 'Event has been added.'
              });
            })
            .catch(function(response) {
              $scope.eventName = '';
              $scope.addForm.$setPristine();
              $alert({
                content: response.data.message
              });
            });
        };
      });

表单模板(html)

<div class="container">
  <div class="panel panel-default">
    <div class="panel-heading">Add Events</div>
    <div class="panel-body">
      <form class="form" method="post" ng-submit="addEvent()" name="addForm">
        <div class="form-group" ng-class="{ 'has-error' : addForm.eventName.$invalid && addForm.eventName.$dirty }">
          <label class="control-label">Name</label>
          <input class="form-control" type="text" name="eventName" ng-model="eventName" placeholder="Select Event Name Here" required autofocus>
          <div class="help-block text-danger" ng-if="addForm.eventName.$dirty" ng-messages="addForm.eventName.$error">
            <div ng-message="required">Event name is required.</div>
          </div>
        </div>
        <button class="btn btn-primary" type="submit" ng-disabled="addForm.$invalid">Add</button>
      </form>
    </div>
  </div>
</div>

app.post()方法

app.post('/api/events', function (req, res, next) {
   var event = new Event();    // create a new instance of the Event model
    event.name = req.body.eventName;  // set the event name (comes from the request)

    event.save(function(err) {
      if (err)
        res.send(err);

      res.json({ message: 'Event Added!' });
    });

});

活动架构

var eventSchema   = new Schema({
  name: String
});
var Event = mongoose.model('Event', eventSchema);

我似乎无法弄清楚为什么我会收到POST / api / events 404错误?

编辑:也添加了GET / api /事件路线

app.get('/api/events', function(req, res, next) {
  Event.find(function(err, events) {
      if (err)
        res.send(err);

      res.json(events);
    });

});

app.get('/api/events/:id', function(req, res, next) {
  Event.findById(req.params.id, function(err, event) {
    if (err) return next(err);
    res.send(event);
  });
});

浏览器日志:

Error: [$injector:unpr] Unknown provider: EventProvider <- Event
http://errors.angularjs.org/1.3.0-beta.17/$injector/unpr?p0=EventProvider%20%3C-%20Event
    at http://localhost:8080/vendor/angular.js:78:12
    at http://localhost:8080/vendor/angular.js:3894:19
    at Object.getService [as get] (http://localhost:8080/vendor/angular.js:4027:39)
    at http://localhost:8080/vendor/angular.js:3899:45
    at getService (http://localhost:8080/vendor/angular.js:4027:39)
    at invoke (http://localhost:8080/vendor/angular.js:4059:13)
    at Object.instantiate (http://localhost:8080/vendor/angular.js:4079:23)
    at http://localhost:8080/vendor/angular.js:7460:28
    at link (http://localhost:8080/vendor/angular-route.js:913:26)
    at nodeLinkFn (http://localhost:8080/vendor/angular.js:6841:13) <div ng-view="" class="{{pageClass}} ng-scope">

2 个答案:

答案 0 :(得分:1)

您似乎正在尝试与角度前端控制器共享您的猫鼬模型。它们彼此独立。

我修改了您的angular controller以使用POST/api/events端点发送$http provider个请求。

angular.module('myapp')
    .controller('AddCtrl', function($scope, $alert, $http) {
        $scope.addEvent = function() {
            $http.post('/api/events', {
                eventName: $scope.eventName
            }).success(function(response) {
                $scope.eventName = '';
                $scope.addForm.$setPristine();
                $alert({
                    content: 'Event has been added.'
                });
            }).error(function(response) {
                $scope.eventName = '';
                $scope.addForm.$setPristine();
                $alert({
                    content: response.data.message
                });
            });
        };
    });

答案 1 :(得分:1)

回答我自己的问题:

我创建了工厂服务

angular.module('myapp')
  .factory('Event', function($resource) {
    return $resource('/api/events/:_id');
  });

现在工作正常。