Querying relations in loopback doesn't work via Angular SDK

70 views
Skip to first unread message

Tejas Bhatt

unread,
Feb 27, 2015, 8:11:03 AM2/27/15
to loopb...@googlegroups.com

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_skillsand /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


What am i doing wrong ?

Miroslav Bajtoš

unread,
Feb 27, 2015, 2:02:13 PM2/27/15
to loopb...@googlegroups.com
You must send the worker id via the "id" parameter in your data, not via the filter.

Something along this lines:

Worker.skills({ id: worker.id });

Of course, it's possible to provide both the worker id and a filter to apply on the skills:

Worker.skills({ id: worker.id, filter: { where: { area: 'IT' } } });

Miroslav

Tejas Bhatt

unread,
Feb 28, 2015, 1:44:46 PM2/28/15
to loopb...@googlegroups.com
Thank you Miroslav, i didn't read the documentation correctly
It works now. 
Reply all
Reply to author
Forward
0 new messages