查询环回中的关系并不能通过Angular SDK工作

时间:2015-02-27 13:09:17

标签: angularjs node.js loopbackjs

我正在尝试查询模型关系 我有两个型号 1.工人 2.技能

工人有很多技能

我可以通过资源管理器查询api http://localhost:3000/explorer/#!/Workers/prototype_get_skills 和/ api / Worker /:id /技能网址 并且它返回给予工人标识的技能列表

当我尝试调用Angular SDK生成的Worker.skills()方法时,会出现问题,我发现404 Not Found错误

以下是我有的Angular实现

 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
        }
    }]);

我还尝试了一个示例loopback-getting-started-intermediate

其中有一个例子

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

然而,这个示例看起来像belongsTo关系,当我尝试修改它无法做到

编辑:添加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": []
}

1 个答案:

答案 0 :(得分:1)

迟到总比没有好。我今天遇到了同样的麻烦。您只需要添加角度依赖性。

 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);

}])