I have a simple express app, that's supposed to provide an endpoint for posting some content. Upon calling $http in my controller I get the following error:
TypeError: Cannot read property 'model' of undefined at new model (C:\Webpages\veryNiceWords\API\node_modules\mongoose\lib\model.js:738:17) at C:\Webpages\veryNiceWords\API\api.js:24:17 at Layer.handle [as handle_request] (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\layer.js:95:5) at next (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\route.js:131:13) at Route.dispatch (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\layer.js:95:5) at C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\index.js:277:22 at Function.process_params (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\index.js:330:12) at next (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\index.js:271:10) at C:\Webpages\veryNiceWords\API\api.js:16:2 at Layer.handle [as handle_request] (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\index.js:312:13) at C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\index.js:280:7 at Function.process_params (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\index.js:330:12) at next (C:\Webpages\veryNiceWords\API\node_modules\express\lib\router\index.js:271:10) at C:\Webpages\veryNiceWords\API\node_modules\body-parser\lib\read.js:129:5
I have no idea what this means, or how to fix it. Can somebody please have a look at my code and point out what I'm doing wrong?
api.js (the express app)
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var Words = require('./models/words.js');
//initialize our express app
var app = express();
//use body parser with JSON
app.use(bodyParser.json());
//middleware for CORS requests
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
//register endpoint
app.post('/API/addWords', function(req, res) {
//get user from request body
var words = req.body;
var newWords = new Words.model({
author: words.author,
source: words.source,
quote: words.quote
});
newWords.save(function(err) {
if (err) throw err;
console.log('words saved!');
});
});
//connect to mongoDB
mongoose.connect('');
//define our server
var server = app.listen(3000, function() {
console.log('api listening on ', server.address().port);
});
words.js (the model required by the api)
// grab the things we need
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// create a schema
var wordsSchema = new Schema({
author: String,
source: String,
quote: String
});
var Words = mongoose.model('Words', wordsSchema);
// make this available to our users in our Node applications
module.exports = Words;
the frontend html:
<div class="container-fluid">
<div class="row">
<div class="form-group">
<form ng-submit="submit()">
<label for="author">Author:</label>
<input type="text" class="form-control" id="author" ng-model="author" placeholder="Maya Angelou">
<br>
<label for="source">Source:</label>
<input type="text" class="form-control" id="source" ng-model="source" placeholder="I know why the caged bird sings">
<br>
<label for="quote">Quote:</label>
<textarea class="form-control" rows="10" id="quote" ng-model="quote" placeholder="There is no greater agony than bearing an untold story inside you"></textarea>
<br>
<div class="col-xs-4 col-xs-offset-4">
<input class="btn btn-default postQuote" type="submit" value="Quote it"></div>
</form>
</div>
</div>
</div>
<div class="container">
<p>{{ author }}</p>
<p>{{ source }}</p>
<p>{{ quote }}</p>
</div>
the front-end controller:
'use strict';
angular.module('frontEndApp')
.controller('AddwordsCtrl', function($scope, $http) {
$scope.submit = function() {
var url = 'http://localhost:3000/API/addWords';
var words = {
author: $scope.author,
source: $scope.source,
quote: $scope.quote
};
$http.post(url, words)
.success(function(res) {
console.log('posted quote!');
})
.error(function(err) {
console.log(err);
});
};
});
Thanks for the help.