云开发如何根据条件选择是否where?
发布于 4 年前 作者 wei41 2854 次浏览 来自 官方Issues

比如传入了几个可选参数,当参数存在时再进行相关的where查询,不存在就跳过,试验过以下方法是无效的:

const collection=db.collection('collection')
var test=collection.where(条件一)
if(条件二符合){
     test.where(条件二)
}
...
if(条件N符合){
    test.where(条件N)
}
test.get({
     success:(res)=>{
          console.log(res)
     }
})

运行结果是,尽管后续根据更多的条件进行了查询,最终也只有条件一生效了。

那么有什么其他办法吗

谢谢

1 回复

学会变通

var wh = {field:‘aaa’} // 默认条件,没有默认即为空 {} 即可

if(xx) wh.field1 = xxxx

if(xx) wh.field2 = xxxx

JSON.stringif(wh) != ‘{}’ ? db.collection().where(wh).get() : void 0

回到顶部