New to AngularJS, using the MEAN stack I'm trying to query a resource, but it seems the query only grabs all, and I can't figure out how to submit a parameter.
For example, a Person can have many phone numbers, and I want to query all phone numbers a person has. But I can't figure out how to pass parameters so it's just pulling all phone numbers in the system regardless of who they belong to.
I can use this to get around it, but is there a way I can just pass a parameter like {person: $stateParams['personId']} to get all phone numbers related to a person?
$scope.find = function() {
PhoneNumber.query(function(phone_numbers) {
var result = $.grep(phone_numbers, function(e){ return e.person == $stateParams['personId']; });
$scope.phone_numbers = result;
});
};
services/phone_number.js
'use strict';
angular.module('mean.phone_numbers').factory('PhoneNumbers', ['$resource',
function($resource) {
return $resource('phone_numbers/:personId', {
personId: '@_id',
}, {
update: {
method: 'PUT'
}
});
}
]);
server/routes/phone_number.js:
module.exports = function(PhoneNumbers, app, auth) {
app.route('/phone_numbers')
.get(phone_numbers.all)
};
server/controller/phone_number.js:
exports.all = function(req, res) {
PhoneNumber.find().sort('-created').populate('person', 'firstname lastname location').exec(function(err, phone_numbers) {
if (err) {
return res.json(500, {
error: 'Cannot list the phone numbers'
});
}
res.json(phone_numbers);
});
};