是否有任何环回 3 功能可以帮助修改然后构建过滤器?

Is there any loopback 3 function that can help in modifying and then building the filters?

我需要默认应用一些过滤器。到目前为止,我已经达到了这一点:

Project.find = function (filter, callback) { return override.apply(this, arguments); }

现在我想在这里也应用我的默认过滤器以及用户传递的过滤器。如何做到这一点?我是否需要手动处理这个问题,或者是否有任何由 loopback 3 提供的内置解决方案?

您可以使用查找上的 'before remote' 挂钩,您可以在其中访问整个请求主体,因此您可以根据系统状态对其进行修改。例如:

Project.beforeRemote('find', function(ctx, next) {
    console.log('whole request: ', ctx.req);
    console.log('request body: ', ctx.req.body);
    console.log('request params: ', ctx.req.params);
    //... add filtering
    next();
});

否则,如果您想对每个 Project.find(..) 调用应用过滤,则需要为 'access' 使用一个操作挂钩,但是您在上下文,例如,你不知道是谁在调用命令。

我强烈建议阅读有关 operational and remote hooks 的优秀环回文档。