Pisix
Pisix

Reputation: 205

Unable to send variable as criteria to waterline query

I build criteria for query as below and I store it in variable called "option"

if (typeof cb.parametre.categorie !== "undefined")
{
  if(typeof options !== "undefined") {
    options =options+ '{categorie: ' + cb.parametre.categorie + '}';
  }else
  {
    options = '{categorie: ' + cb.parametre.categorie + '}';

  }
}
if (typeof cb.parametre.localisation !== "undefined")
{
  if(typeof options !== "undefined")
  {
    options=options+',{nomVille:'+cb.parametre.localisation.address_components[0].long_name+'}';
  }
  else
  {
    options='{nomVille:'+cb.parametre.localisation.address_components[0].long_name+'}';
  }
}
if(cb.parametre.motclef)
{
    if(typeof options !== "undefined")
    {
      options=options+",{or: [{titre:{'like':'%"+cb.parametre.motclef+"%'}},{details:{'like':'%"+cb.parametre.motclef+"%'}}]}";
    }else
    {
      options="{or: [{titre:{'like':'%"+cb.parametre.motclef+"%'}},{details:{'like':'%"+cb.parametre.motclef+"%'}}]}";
    }
}

after the if instructions options={categorie: 1},{or: [{titre:{'like':'%Voiture%'}},{details:{'like':'%Voiture%'}}]}

But when I pass criteria "options" in my query I get not result

 Article.find().where(options)
                      .where({prix:{'>':prixmin, '<':prixmax}})
          .sort(cb.parametre.filterBy)
          .populate('images').populate('devise').exec(function(err,result){
          if(result)
          {
            val(null,result);
          }
          if(err)
          {
            val(err,null);
          }
        })

Conversely If I send directly the value of options as criteria as shown below

 Article.find().where({categorie: 1},{or: [{titre:{'like':'%Voiture%'}},{details:{'like':'%Voiture%'}}]})
                      .where({prix:{'>':prixmin, '<':prixmax}})
          .sort(cb.parametre.filterBy)
          .populate('images').populate('devise').exec(function(err,result){
          if(result)
          {
            val(null,result);
          }
          if(err)
          {
            val(err,null);
          }
        })

I get results and I dont know why because for my side it's same. How can I do to fix this issue ?

Upvotes: 1

Views: 70

Answers (1)

Meeker
Meeker

Reputation: 5979

Your options variable that you are creating is a string

options = '{categorie: ' + cb.parametre.categorie + '}';

This is wrong, you need to create an object

options = { categorie : cb.parametre.categorie }

Here is a cleaner version of what you were trying to do

var options = options || {}

if (typeof cb.parametre.categorie !== "undefined") {
    options.categorie = cb.parametre.categorie;
}

if (typeof cb.parametre.localisation !== "undefined") {
    options.nomVille = cb.parametre.localisation.address_components[0].long_name;
}

if(cb.parametre.motclef) {
  options.or = [
    { titre: { 'contains':cb.parametre.motclef } } , 
    { details:{ 'contains':cb.parametre.motclef } }
  ]
}

Upvotes: 2

Related Questions