18.05.2016 Views

Mittwoch, 18. Mai, 2016

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Finishing Angular TODO application and deploying to production 112<br />

If this looks foreign, then I beg you please go and brush up on Mongoose in our third tutorial which<br />

was all about MongoDB, Mongoose and Passport Authentication²⁰¹.<br />

In this code you created a Mongoose Todo model with five model fields (created, title, comment,<br />

creator, completed) whose meaning should speak for itself. An option worth mentioning additionally<br />

is that we made a reference to the User model object for the creator field so that we’ll have the info<br />

user actually created the certain todo.<br />

Next, in the config/mongoose.js file and add the following line:<br />

1 require('../app/models/todo.server.model');<br />

Just for your reference, the whole contents of the file should look like this:<br />

1 var config = require('./config'),<br />

2 mongoose = require('mongoose');<br />

3<br />

4 module.exports = function() {<br />

5 var db = mongoose.connect(config.db);<br />

6<br />

7 require('../app/models/user.server.model');<br />

8 require('../app/models/todo.server.model');<br />

9<br />

10 return db;<br />

11 };<br />

Control all the things<br />

Now you’re going to set up the controller for your CRUD part of the TODO thingies. In the<br />

app/controllers folder create a new file todos.server.controller.js with the full following content:<br />

1 var mongoose = require('mongoose'),<br />

2 Todo = mongoose.model('Todo');<br />

3<br />

4 var getErrorMessage = function(err) {<br />

5 if (err.errors) {<br />

6 for (var errName in err.errors) {<br />

7 if (err.errors[errName].message) return err.errors[errName].message;<br />

8 }<br />

9 } else {<br />

10 return 'Unknown server error';<br />

²⁰¹https://hackhands.com/mongodb-crud-mvc-way-with-passport-authentication/

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!