Reputation: 213
I am unable to POST a response from an AngularJs form to my Mongo DB database. Here is the code snippet for
Controller :
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
});
});
};
});
Template of form (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() method :
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!' });
});
});
Event Schema :
var eventSchema = new Schema({
name: String
});
var Event = mongoose.model('Event', eventSchema);
I can't seem to figure out why am I getting POST /api/events 404 error ?
EDIT : Added GET /api/event routes too
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);
});
});
Browser Log :
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">
Upvotes: 1
Views: 4236
Reputation: 213
Answering my own question :
I created a factory service
angular.module('myapp')
.factory('Event', function($resource) {
return $resource('/api/events/:_id');
});
It's working fine now.
Upvotes: 1
Reputation: 276
It appears you're trying to share your mongoose model with your angular front-end controller. They are independent of each other.
I modified your angular controller
to send a POST
request to the /api/events
endpoint using the $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
});
});
};
});
Upvotes: 1