-
Notifications
You must be signed in to change notification settings - Fork 2
Angular Make Useful APIs
There are two more things you have to do before this to be useful to you, however. Say you want to show all the things associated with the username requested with that page: you must first
- Have a "username" or "owner" field in your thing schema at
/server/api/thing/thing.model.js
- Write a custom route in
/server/api/thing/index.js
to catch a request for a specific username. The request from your frontend might look something like:
$http.get('/api/things/' + username).success( … )
so you'll add a line into your index.js
like:
router.get('/:user', controller.indexUser);
and then in thing.controller.js
you'll write an exports.indexUser function like so:
exports.indexUser = function(req, res) {
Thing.find({owner:req.params.user}, function (err, things) {
if(err) return res.send(500, err);
res.json(200, things);
});
};
Warning!!! this method only works right if usernames are absolutely unique between users. The default authentication system that comes with the angular-fullstack generator does not have unique usernames, so you're probably better off using the _user.id field to determine unique users in your database for now, unless you want to implement unique user names yourself by altering your /api/user/user.model.js
, /api/user/user.controller.js
, and your /app/client/account/signup/signup.controller.js
. Thankfully, you should know how to go about doing all that after reading this guide!
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links