收藏
回答

【已解决】.where 不支持表达式,那么如何避免外围写一堆if else?

//数据库 user
{
  uid: 666,
  name: "柚子",
  lv: 10,
  group: 888
},
{
  ......
}


// GetUserInfo.js
GetUI(o){

  let uid = Number(o.uid) || [] // 666
  let name = o.name || [] // '柚子'
  let lv = Number(o.lv) || [] // 空
  let group = o.group || [] // 空

  // group & lv 为空时
  if (group == '' && lv == '') {
    db.collection('user')
      .where({
        uid: _.in(uid), // 筛id
        name: _.in(name) // 筛名字
      })
      .get()
      .then(res => {
        console.log("返回:", res.data)
      })
      .catch(err => {
        console.log("错误:", err);
      })
  } else if (group != '' && lv == '') { //group不为空 & lv 为空时
    db.collection('user')
      .where({
        uid: _.in(uid), // 筛id
        name: _.in(name), // 筛名
        group: _.in(group) // 筛组
      })
      .get()
      .then(res => {
        console.log("返回:", res.data)
      })
      .catch(err => {
        console.log("错误:", err);
      })
  } else if (group == '' && lv != '') { //group为空 & lv 不为空时
    db.collection('user')
      .where({
        uid: _.in(uid), // 筛id
        name: _.in(name), // 筛名
        lv: _.in(lv) // 筛级
      })
      .get()
      .then(res => {
        console.log("返回:", res.data)
      })
      .catch(err => {
        console.log("错误:", err);
      })
  }
}


 .where{} 参数值为空时,查询不到数据~
  https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/database/dbscript.html
 文档说不支持各种表达式,.......
 我不想写这么多if else......
 有什么办法能不写N个if else???
 感谢~

最后一次编辑于  2020-10-05
回答关注问题邀请回答
收藏

3 个回答

  • o0o有脾气的酸奶
    o0o有脾气的酸奶
    2020-07-21

    其实写if else 也没事,反倒代码比较容易看懂

    也可以写得很简洁,也可以不用写那么多let,但是前提示会用还要看得懂,让别人也看得懂

    async GetUI(o){
      let res = [],wh = {}, isEmptyWhere = !0, 
        { uid = !1, name = !1, lv = !1, group = !1 } = o || {}, arrInit
      try{
        arrInit = (o, type)=>(!Array.isArray(o) && (o = [o]), o.map(v=>type?type(v):v))
    
        lv && (wh.lv = _.in(arrInit(lv, Number)))
        uid && (wh.uid = _.in(arrInit(uid, Number)))
        name && (wh.name = _.in(arrInit(name, String)))
        group && (wh.group = _.in(arrInit(group, String)))
    
        // 查询条件是否为为空
        isEmptyWhere = JSON.stringify(wh) != '{}'
        res = isEmptyWhere ? {} : await db.collection('user').where(wh).get()
        res = res.data ? res.data : []
        console.log("返回:", res)
      }catch(e){
        console.log("错误:", e)
      }
      return res
    }
    

    若认为该回答有用,给回答者点个[ 有用 ],让答案帮助更多的人

    2020-07-21
    有用 1
    回复 1
    • iblackly
      iblackly
      2020-07-21
      感谢~
      2020-07-21
      回复
  • Mr.Zhao
    Mr.Zhao
    2020-07-20

    只需要组装where condition就行了

    2020-07-20
    有用 1
    回复 3
    • iblackly
      iblackly
      2020-07-20
      大佬~有参考吗?谢谢~
      2020-07-20
      回复
    • Mr.Zhao
      Mr.Zhao
      2020-07-20回复iblackly
      2020-07-20
      1
      回复
    • iblackly
      iblackly
      2020-07-20回复Mr.Zhao
      👍太赞了~谢谢大佬~感谢感谢~
      2020-07-20
      回复
  • Johanna J.
    Johanna J.
    发表于移动端
    2022-05-22
    在大佬的基础上修改一下,或许可以设conditon为全部搜索条件,然后如果某项为空,delete那个搜索条件?
    2022-05-22
    有用
    回复
登录 后发表内容
问题标签