Loopback-创建一种使其可以在两个不同模型中访问的方法

问题描述 投票:0回答:1

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

模型报告:

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

我该如何解决?谢谢

javascript rest loopback
1个回答
0
投票

您可以使用“接受”来定义路径参数。并且应该相应地更改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" }
    });
© www.soinside.com 2019 - 2024. All rights reserved.