用于GET /项目的LoopBack过滤

时间:2015-07-08 11:58:33

标签: loopbackjs

使用Project.find()或GET / projects我只想获得当前用户项目。这个过滤应该在哪里完成?我在project.json中试过acls:

"accessType": "*",
"principalType": "ROLE",
"principalId": "teamMember",
"permission": "ALLOW"

和注册的解析器。但是我只能将它用于findById()或GET / projects /:id,而不是列出所有项目。

我还在project.js中作为观察员尝试过:

Project.observe('access', function(ctx, next){
    ctx.query.where.memberId = 1;
    next();
});

等..

这应该如何完成?我还在这里研究了类似案例的Loopbacks教程:http://docs.strongloop.com/display/public/LB/Tutorial%3A+access+control

虽然这不是我想做的事情,但我仍然坚持:)

1 个答案:

答案 0 :(得分:0)

您可以在get方法中使用where filter来实现您想要的效果。在您的项目模型中,您可能拥有userId。你要做的是添加"其中"使用userId过滤您的请求

/项目?过滤[其中] [用户id] = 1

,或者如果你想使用节点API,你可以这样做:

Project.find({
        where: {userId: 1}
    }, 
    function(err, projects){
        //do something with projects
     });

<强>更新 如果要在READ操作上修改查询,则需要使用操作挂钩。你走在正确的轨道上。

  Project.observe('access', function(ctx, next){
    ctx.query = {"where": {userId: 1}}; //define your query as needed
    next();
  });