0

I am trying to query a model relation I have two models 1. Workers 2. Skills

Worker hasMany Skills

I can query the api via the explorer http://localhost:3000/explorer/#!/Workers/prototype_get_skills and /api/Worker/:id/Skills url and it gives back a list of skills for a give Worker Id

The issue happens when i try to call the Worker.skills() method generated by Angular SDK where i get a 404 Not Found Error

Below is the Angular implementation that i have

 angular.module('worker-dashboard').factory('WorkerDashboardSkillService',['Worker',
    function(Worker){
        function getWorkerSkills(worker){
            return  Worker.skills({
                    filter:
                    {
                        where:
                        {
                            "workerId" : worker
                        }
                    }
                },function(data){
                    console.log(data);
                },
                function(err){
                    console.log(err);
                })
        }
        function addWorkerSkills(worker,skill){
            return  Worker.skills.create(
                {
                    "skillName": skill.name,
                    //TODO chabge below
                    "skillCategory": skill.name,
                    "workerId": worker
                },function(data){
                    console.log(data);
                },
                function(err){
                    console.log(err);
                })
        }

        return{
            getWorkerSkills : getWorkerSkills,
            addWorkerSkills : addWorkerSkills
        }
    }]);

I also tried an example loopback-getting-started-intermediate

Which has an example

$scope.reviews = Review.find({
filter: {
where: {
publisherId: $rootScope.currentUser.id
},
include: [
'coffeeShop',
'reviewer'
]
}
});

However this example is looks like for the belongsTo relation and when i tried modifying it couldn't do it

Edit : Adding Worker.json

{
  "name": "Worker",
  "base": "User",
  "idInjection": true,
  "properties": {
    "workerName": {
      "type": "string"
    },
    "workerFirstName": {
      "type": "string"
    },
    "workerLastName": {
      "type": "string"
    },
    "isWorkerBlackListed": {
      "type": "boolean"
    },
    "workerBlacklistedByClient": {
      "type": [
        "string"
      ]
    }
  },
  "validations": [],
  "relations": {
    "skills": {
      "type": "hasMany",
      "model": "Skills",
      "foreignKey": "workerId"
    }
  },
  "acls": [
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW"
    }
  ],
  "methods": []
}

Skills.json

{
  "name": "Skills",
  "base": "PersistedModel",
  "idInjection": true,
  "properties": {
    "skillName": {
      "type": "string"
    },
    "skillCategory": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "worker": {
      "type": "belongsTo",
      "model": "Worker",
      "foreignKey": ""
    }
  },
  "acls": [
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW"
    }
  ],
  "methods": []
}
2
  • Can you show us your Worker.json file? Commented Feb 27, 2015 at 14:34
  • @jakerella i added both worker.json and skills.json above Commented Feb 27, 2015 at 14:38

1 Answer 1

1

Better late than never. I experienced the same trouble today. You just need to add the angular dependency.

 angular 
 .module('app')
  //inject all the model you need
  .controller('ListBooksController', ['$scope', '$state', 'Book', 'Collection', 'Author', function($scope,
$state, Book, Collection, Author) {

  $scope.books = Book.find({
  filter: {
    include: [
     'author'
    //put the model you want to retrieve
    ]
  }
  });
  console.log($scope.books);

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

1 Comment

i didn't need to add Author for this to work, but otherwise it worked great. Thanks.

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.