【问题标题】:loopback - query related modelloopback - 查询相关模型
【发布时间】:2016-06-18 17:07:29
【问题描述】:

我有一个 Client 模型,它通过 InstrumentTracking 模型(即“通过”关系)与 Instrument 模型有 hasMany 关系。 如何从控制器代码中获取特定客户端的所有工具?

我尝试使用给我“未定义”的 client.instruments,我尝试使用 include 'instrument' 或 'Instrument' 或 'InstrumentTracking' 或 'instrumentTracking' 进行查找,没有任何效果,任何帮助将不胜感激。

【问题讨论】:

  • 你能分享你获取仪器的代码吗?

标签: loopbackjs angular-loopback


【解决方案1】:

好的,所以似乎包含应该包含关系本身的名称(在关系中的“客户端”的 json 文件中,我看到有很多到工具的关系被称为“工具”),太糟糕了 strongloop 未能在他们的文档中指定它。

最终这成功了:

InstrumentTracking.find({  filter: { where: {userID:$rootScope.currentUser.id}, include:['instrument'] }})
        .$promise
        .then(function(foundInst) {
                var instrument = foundInst[0].instrument;
                console.log("foundInst="+JSON.stringify(instrument));
                $scope.instrumentTracking = instrument;
            }
        );

但这不起作用(返回空仪器数组):

        Client
        .find({  filter: { where: {id: $rootScope.currentUser.id}}})
        .$promise
        .then(function(foundUsers) {
            console.log("foundUsers="+JSON.stringify(foundUsers));
            console.log("found == " +JSON.stringify(foundUsers[0]));
            console.log(" foundUser.instruments = " +  foundUsers[0].instruments.find({}).$promise
                    .then(function(foundInst) {
                        console.log("foundInst == " +JSON.stringify(foundInst));
                    }));
        });

由于某种原因,当我尝试从客户端模型获取仪器时,它总是返回空数组,即使我使用 API Explorer。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    相关资源
    最近更新 更多