【问题标题】:Loopback include unrelated lists环回包括不相关的列表
【发布时间】:2015-07-24 13:32:26
【问题描述】:

在 Loopback 中,查询数据时很容易包含关系对象。例如,可以使用 include 过滤器在一次调用中包含属于博客文章的所有 cmets。

但就我而言,我想获取没有关系的数据。

我有一个用户详情页面。在该页面上,用户可以选择username,还有一个下拉列表,用户可以从中选择他所在的国家/地区。

所以从客户端我做了类似的事情:

Country.find().$promise.then(function(countryData) {
    $scope.countries = countryData;
});
Player.find().$promise.then(function(playerData) {
  $scope.player = playerData;
}

但是,如果我有更多想要填写的列表怎么办?比如,citystatecolors

然后我必须打很多单独的电话。

有没有办法在一次调用中包含所有这些数据,即使它们没有关系?像这样的:

Player.find({ filter: { include: ["countries", "colors"] } }).$promise.then(function(data) {
   // some stuff
}

【问题讨论】:

    标签: loopbackjs strongloop loopback


    【解决方案1】:

    您可能想尝试使用记录在 here 中的 Where 过滤器

    查询两个特定事物的示例如下:

    Post.find({where: {and: [{title: 'My Post'}, {content: 'Hello'}]}}, 
              function (err, posts) {
                ...
    });
    

    【讨论】:

      【解决方案2】:

      您可以在其中一个模型上创建一个远程方法,该方法在内部进行调用并将它们打包为您备份。

      如果不使用 ES6 则使用一些 promise 库等待所有然后返回

      Model.getAll = function(next) {
        var promises = [];
      
        promises.push(Model.app.models.Country.find());
        promises.push(Model.app.models.Player.find());
        promises.push(Model.app.models.Color.find());
      
        Promise.all(promises)
          .then(function(results) {
            next(results);
          });
      }
      
      /**
        Register your remote method here
      */
      

      【讨论】:

        【解决方案3】:

        您可以在其中一个模型上创建一个远程方法,该方法在内部进行调用并将它们打包为您备份。

        如果不使用 ES6 则使用一些 promise 库等待所有然后返回

        Model.getAll = function(next) {
          var promises = [];
        
          promises.push(Model.app.models.Country.find());
          promises.push(Model.app.models.Player.find());
          promises.push(Model.app.models.Color.find());
        
          Promise.all(promises)
            .then(function(results) {
              next(results);
            });
        }
        
        /**
          Register your remote method here
        */
        

        我有问题并尝试使用此解决方案,但我收到错误“因多个错误而失败,请参阅details 了解更多信息。”。使用 promise.all 时,Loopback 似乎存在错误

        【讨论】:

          猜你喜欢
          • 2022-06-17
          • 2021-08-19
          • 2019-10-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-22
          • 1970-01-01
          相关资源
          最近更新 更多