0

I have a Client model with hasMany relation to Instrument model through InstrumentTracking model (that is a "through" relation). How do I get all the instruments of a specific Client from a controller code?

I tried using client.instruments which gave me "undefined", i tried a find with include 'instrument' or 'Instrument' or 'InstrumentTracking' or 'instrumentTracking', nothing at all works, any help will be much appreciated.

Gilad Baruchian
  • 930
  • 3
  • 14
  • 30

1 Answers1

0

Ok so it seems that the include should contain the name of the relation itself (in the json file of "Client" in relations I saw that the has many to insturments relation is called "instrument"), too bad strongloop failed to specify it in their documentation.

eventually this worked :

InstrumentTracking.find({  filter: { where: {userID:$rootScope.currentUser.id}, include:['instrument'] }})
        .$promise
        .then(function(foundInst) {
                var instrument = foundInst[0].instrument;
                console.log("foundInst="+JSON.stringify(instrument));
                $scope.instrumentTracking = instrument;
            }
        );

but this didnt work (returned empty instrument array):

        Client
        .find({  filter: { where: {id: $rootScope.currentUser.id}}})
        .$promise
        .then(function(foundUsers) {
            console.log("foundUsers="+JSON.stringify(foundUsers));
            console.log("found == " +JSON.stringify(foundUsers[0]));
            console.log(" foundUser.instruments = " +  foundUsers[0].instruments.find({}).$promise
                    .then(function(foundInst) {
                        console.log("foundInst == " +JSON.stringify(foundInst));
                    }));
        });

For some reason when I try to get the instruments from client model it always returns empty array, even if I use the API Explorer.

Gilad Baruchian
  • 930
  • 3
  • 14
  • 30