-2

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);

  });
};
avoliva
  • 3,181
  • 5
  • 23
  • 37
  • 1
    If you are using [underscore/loadash](https://lodash.com/docs#where) instead of using jquery you could do `_.where(phone_numbers, {person: $stateParams['personId']});` Or create your own custom static jquery function. – PSL Oct 25 '14 at 02:51
  • isn't lodash only usable on the node side of things? – avoliva Oct 25 '14 at 03:02
  • 1
    Not really.. You can use it on client side as well.. It has just great set of utilities which can run where js can... :) Also [check this answer](http://stackoverflow.com/questions/13789618/differences-between-lodash-and-underscore) – PSL Oct 25 '14 at 03:04
  • You need to develop a bit more: have you an express `PhoneNumber` route? What does it look like? What is the `PhoneNumber` service? – Preview Oct 27 '14 at 19:21
  • @Aperçu added more details. After playing the stack some more, I'm guessing the answer lies in the ember controller, and I somehow pass the personId to that to look up with Mongoose? I'm having a hard time debugging the node side of things, but I think I'll try something like that. Any help or tips would be appeciated. – avoliva Oct 28 '14 at 02:28

3 Answers3

2

To do this in the MEAN way, you'll have to create a new route in your API like this:

module.exports = function(PhoneNumbers, app, auth) {

  app.route('/phone_numbers').get(phone_numbers.all);
  app.route('/phone_numbers/:id').get(phone_numbers.one);

};

And then define the associated method in your controller, where you will be able to query only a specific id in the params of your request:

exports.one = function(req, res) {
  PhoneNumber.findById(req.params.id)
    .populate('person', 'firstname lastname location')
    .exec(function(err, phone_number) {
      if (err) {
        return res.json(500, { error: 'Cannot find the phone number' });
      }
      res.json(phone_numbers);
   });
};

Now, you can search for a specific PhoneNumber with your ressource using its id

$scope.findOne = function (id) {
  PhoneNumber.get({ personId: id }, function(phone_number) {
    // Do what you want with the result
  });
};
Preview
  • 35,317
  • 10
  • 92
  • 112
0

you can use a for loop to iterate through the list

   PhoneNumber.query(function(phone_numbers) {
        var result = [];
        angular.forEach(phone_number, function(e) {
                if(e.person == $stateParams['personId']) {
                      result.push(e);
                }
        });
        $scope.phone_numbers = result;
    });

or if you are using underscore or lodash, you can do

 PhoneNumber.query(function(phone_numbers) {  
   var result = _.where(phone_numbers, {'person' : $stateParams['personId']});
   $scope.phone_numbers = result;
 }
harishr
  • 17,807
  • 9
  • 78
  • 125
0

Instead of .find(), add the condition:

.find({ person: req.body.personId })
KyleK
  • 4,643
  • 17
  • 33