【问题标题】:Loopback - Create a method to make it accessible in two different modelsLoopback - 创建一种方法以使其可在两种不同的模型中访问
【发布时间】:2019-09-29 18:38:25
【问题描述】:

我正在使用环回。在名为 Report 的模型中创建一个名为 stats 的新方法。我想在另一个名为 Acount 的模型中创建相同的方法,但参数是 id。

模型报告:

Report.remoteMethod("stats", {
        accepts: [],
        returns: { arg: "features", type: "Object" },
        http: { verb: "get", path: "/stats" }
    });

我该如何解决这个问题? 谢谢

【问题讨论】:

    标签: javascript rest loopback


    【解决方案1】:

    您可以使用“accepts”来定义路径参数。并且http路径应该相应地改变,“/stats/:id”或者“/:id/stats”

    Account.stats = function(id, cb) {
        cb(null, 'ID ' + id);
    };
    
    Account.remoteMethod("stats", {
          accepts: [
        { arg: "id", type: "number", http: { source: "path" } }
        ],
          returns: { arg: "features", type: "Object" },
            http: { verb: "get", path: "/stats/:id" }
        });
    

    【讨论】:

    • 在模型报告中我有 stats 方法。在模型帐户中你告诉我的。我运行0.0.0.0:3000/api/Accounts/390/stats 得到这个: {"features": "ID 390"} 我希望得到与报告/统计相同的结果,但使用 id 参数过滤器
    • @josego 我只是举了一个例子,我实际上并不知道你所期望的真实场景。你得到了{"features": "ID 390"},因为在 Account.stats 方法中,回调返回了cb(null, 'ID ' + id);
    猜你喜欢
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多