Angularjs 无法发布/api节点
我无法将AngularJs表单的响应发布到我的Mongo DB数据库。下面是的代码片段 控制器:Angularjs 无法发布/api节点,angularjs,node.js,mongodb,express,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
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
});
});
};
});
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);
表单模板(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>
事件模式:
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
});
});
};
});
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">
错误:[$injector:unpr]未知提供程序:EventProvider似乎您正试图与角度前端控制器共享mongoose模型。它们相互独立
我修改了角度控制器
,使用$http提供程序
向/api/events
端点发送POST
请求
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
});
});
};
});
回答我自己的问题:
我创建了一个工厂服务
angular.module('myapp')
.factory('Event', function($resource) {
return $resource('/api/events/:_id');
});
它现在工作正常。发布您的事件服务您是否可以共享您的任何浏览器日志?是res.send(err)代码>这是发送erorr的原因?除了事件
服务之外,如果能看到其他相关的快速路由,那就太好了。在请求发送到您的/api/events
处理程序之前抛出404的东西。它只是发送POST/api/events 404 389msI在问题@MattG中添加了它们