2

I'm trying to search inside JSON file by the _id, then only return the team_members array as you see below is an example of one object inside the file

    {
        "_id": "5769484bb3ef5c696c5686d0",
        "name": "XXXXX",
        "family": "XXXXX Products",
        "description": "XXXXXXXXXXXXXXXX",
        "image": "http://localhost/img/proudct6.png",
        "__v": 8,
        "team_members": [{
            "_id": "57694567b3ef5c696c5686c2",
            "name": "XXXXXXX",
            "description": "team member",
            "image": "http://localhost/img/1.png",
            "mobile_number": "XXXXXXXXXXXXXXX",
            "specialty": "Developer",
            "summary": "only for test"
        }, {
            "_id": "57694567b3ef5c678c5686c6",
            "name": "XXXXXXX",
            "description": "team member",
            "image": "http://localhost/img/1.png",
            "mobile_number": "XXXXXXXXXXXXXXX",
            "specialty": "Developer",
            "summary": "only for test"
        }]
    }

and here my code:

this.getProductReferences = function(productId){
var dfd = $q.defer();
$http.get('database.json').success(function(database) {
  var product = _.find(database.products, function(product){ return product._id == productId; });
  var references =product.references;

  dfd.resolve(references);
});
return dfd.promise;
};
Sideeq Youssef
  • 903
  • 2
  • 10
  • 24

3 Answers3

1

Here's a working plunk. Since $http is promise based you can remove the dfd promise and return it directly. Then handle the promise resolution from your calling func.

Controller

  var self = this;

  this.getProductReferences = function(productId) {
    return $http.get('database.json').success(function(database) {
      var product = _.find(database.products, function(product) {
        return product._id == productId;
      });
      return product.references;
    });
  };

  // this could be wrapped in a func that is called from your html
  this.getProductReferences("5769484bb3ef5c696c5686d0").then(function(result) {
    self.references = result.data.team_members;
  })

I chose to pass "5769484bb3ef5c696c5686d0" in as productId just for this example

jbrown
  • 3,025
  • 1
  • 15
  • 22
0

You can use $filter service.

var object = $filter('filter')(database, {'_id': '5769484bb3ef5c696c5686d0'});
console.log(object.team_members) // here is your team members array
yogurt
  • 380
  • 1
  • 10
0

Thanks all, it's working with me know, some one write the answer and then delete and i don't know why but i try before and it's work..

  this.getProductReferences = function(productId) {
   return $http.get('database.json').success(function(database) {
     var product = _.find(database.products, function(product) {
       return product._id == productId;
     });
     return product.team_members;
   });
 };

 this.getProductReferences(productId).then(function(references) {
  $scope.references = references.data.team_members;
 });
Sideeq Youssef
  • 903
  • 2
  • 10
  • 24
  • I had deleted it since I needed to make a change but couldn't get to it right away. I've undeleted it now. – jbrown Jun 22 '16 at 13:41