无法将variables作为标准发送给waterline查询

我build立的查询条件如下,我把它存储在一个名为“选项”

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+"%'}}]}"; } } 

if指令options={categorie: 1},{or: [{titre:{'like':'%Voiture%'}},{details:{'like':'%Voiture%'}}]}

但是,当我通过条件“选项”在我的查询中得不到结果

  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); } }) 

相反如果我直接发送选项的价值作为标准如下所示

  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); } }) 

我得到的结果,我不知道为什么,因为我身边是一样的。 我该如何解决这个问题?

你正在创build的选项variables是一个string

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

这是错误的,你需要创build一个对象

options = { categorie : cb.parametre.categorie }

这是你正在尝试做的更清洁的版本

 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 } } ] }