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;
};
8
  • 1
    I dont think that is possible, try itrating over the array - stackoverflow.com/questions/19590063/… Commented Jun 22, 2016 at 12:30
  • _.find works on array, not on object Commented Jun 22, 2016 at 12:31
  • database.products is an array of product objects ? Commented Jun 22, 2016 at 12:38
  • @gaurav5430 yes it's an array. Commented Jun 22, 2016 at 12:56
  • @PankajParkar actually the files content an array of products Commented Jun 22, 2016 at 12:56

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

0

You can use $filter service.

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

3 Comments

filter works on array, not sure if database is an array
He wrote that the example is an object inside the file. So I thought there should be many objects in an array in this json file.
in that case his own code should work as well (with some modifications maybe)
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;
 });

1 Comment

I had deleted it since I needed to make a change but couldn't get to it right away. I've undeleted it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.