404answernotfound
404answernotfound

Reputation: 771

loopback Find() "where" clause not returning expected results

I am using loopback to serve the API for my application and I tried to change the GET request for some data.

As of now the query fetches all the results for a particular API:

People.find({
    where: {
      'town': 'name of a town'
    }
  }).$promise
  // Promise is fulfilled and people returned
  .then(function(results) {
    $scope.people = results;
  })
  // Promise is rejected and error catched
  .catch(function(err) {
    $scope.errors.PeopleFind = JSON.stringify(err.data.error.message ?
      err.data.error.message :
      err.data.error.errmsg
    );
  }); 

I already tried with adding single quotes to the where clause or to do something like .find({ where : { town : 'name of a town' }}.

No matter where I put the quotes the results are always the whole package. How would I query for just the results that I'm interested in?

Thanks in advance

Upvotes: 3

Views: 4495

Answers (3)

ritesh
ritesh

Reputation: 197

This is the common mistake, please check your respective model People.json config file, and make town: {"type":"string"}

Upvotes: 0

Ali Bahrami
Ali Bahrami

Reputation: 503

use like like this:

People.find({where: {Town : like: {currentUserTown}}}, function(err, res){
//code goes here.
})

and be sure you've added strictObjectIDCoercion in json file:

{
  "name": "People",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true,
    "strictObjectIDCoercion": true
  },
  "properties": {
 // rest of json file ...

hope this helps.

Upvotes: 3

404answernotfound
404answernotfound

Reputation: 771

I found the answer thanks to a collegue, I'll write here the answer


People
            .find({
                filter: {
                    where: {Town : currentUserTown}
                }
            })

The documentation for the loopback framework did not state that you needed to apply a filter object to actually filter the results, in fact you can check the documentation with this example they wrote:

Cars.find({where: {carClass:'fullsize'}});

Before the where clause object you need to write the filter object that contains the clause, that should solve the problem with the query.

Upvotes: 11

Related Questions