无法将变量作为条件发送到水线查询
Unable to send variable as criteria to waterline query
我按如下方式构建查询条件,并将其存储在名为 "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+"%'}}]}";
}
}
在 if
指令之后 options={categorie: 1},{or: [{titre:{'like':'%Voiture%'}},{details:{'like':'%Voiture%'}}]}
但是当我在查询中传递标准 "options" 时,我没有得到结果
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);
}
})
我得到了结果,但我不知道为什么,因为我这边是一样的。
我该如何解决这个问题?
您正在创建的选项变量是一个字符串
options = '{categorie: ' + cb.parametre.categorie + '}';
这是错误的,你需要创建一个对象
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 } }
]
}
我按如下方式构建查询条件,并将其存储在名为 "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+"%'}}]}";
}
}
在 if
指令之后 options={categorie: 1},{or: [{titre:{'like':'%Voiture%'}},{details:{'like':'%Voiture%'}}]}
但是当我在查询中传递标准 "options" 时,我没有得到结果
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);
}
})
我得到了结果,但我不知道为什么,因为我这边是一样的。 我该如何解决这个问题?
您正在创建的选项变量是一个字符串
options = '{categorie: ' + cb.parametre.categorie + '}';
这是错误的,你需要创建一个对象
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 } }
]
}