1

In Loopback it is easy to include relational objects when querying for data. For example, one can include all the comments that belong to a blog post in a single call using the include filter.

But in my case I want to get data that doesn't have a relation.

I have a User Detail page. On that page a user can choose a username and there's also a dropdown list where a user can choose from what country he is.

So from the client side I do something like:

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

But what if I get more lists that I want to fill? Like, city, state, colors etc.

Then I'd have to make a lot of separate calls.

Is there a way to include all this data in one call, eventhough they have no relation? Something like this:

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

3 Answers 3

0

You may want to try using the Where filter as documented here

An example of this for querying two specific things would be:

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

You could create a remote method on one of your models that makes the calls internally and packages them back up for you.

Use some promise library if not using ES6 to wait for all and then return

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
*/
0

You could create a remote method on one of your models that makes the calls internally and packages them back up for you.

Use some promise library if not using ES6 to wait for all and then return

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
*/

I have problem and try with this solution but i get error "Failed with multiple errors, see details for more information.". It seems like there is bug on Loopback while using promise.all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.