【问题标题】:Overriding Relation Remote Method In Strongloop Loopback在 Strongloop 环回中覆盖关系远程方法
【发布时间】:2019-09-16 08:58:06
【问题描述】:

我有一个用户模型和一个追随者模型,它与用户具有 HasManyThrough 关系,用于追随者和被追随者。 如何更改默认的 __get__followers 方法参数?

【问题讨论】:

    标签: node.js loopbackjs strongloop


    【解决方案1】:

    我发现我可以按正常方式添加一个新的远程方法。

    loopback.remoteMethod(
                UserModel.prototype.getFollows,
                {
                    description: 'Get the users who are followed by the user',
                    accepts: [
                        {arg: 'page', type: 'Number', http: {source: 'query'}, required: true}
                    ],
                    returns: {arg: 'data', type: 'object'},
                    http: {verb: 'get', path: '/follows'},
                    isStatic: false,
                }
        );
    

    【讨论】:

      【解决方案2】:

      另一种方式。树有很多叶子,我们想要覆盖树的__get__leaves 关系处理程序。

      /**
       * Before Remote handler
       *
       * @param {Object} ctx - Context
       * @param {Function} next - Callback function
       */
      Tree.beforeRemote('**', function (ctx, unused, next) {
        if (ctx.method.name === '__get__leaves') {
          return Tree.getLeaves(ctx)
        }
      
        next()
      })
      
      /**
       * Override Relation handler
       *
       * @param {Object} ctx - Context
       */
      Tree.getLeaves = (ctx) => {
        ctx.res.sendStatus(200)
      }
      

      【讨论】:

        猜你喜欢
        • 2015-12-15
        • 1970-01-01
        • 2015-09-18
        • 1970-01-01
        • 2017-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-04
        相关资源
        最近更新 更多