zoukankan      html  css  js  c++  java
  • 小程序 .where 不支持表达式,那么如何避免外围写一堆if else?

    昨晚的原帖:https://developers.weixin.qq.com/community/develop/doc/000c8c3f34018010ecaafe3905a400

     1 //数据库 user
     2 {
     3   uid: 666,
     4   name: "柚子",
     5   lv: 10,
     6   group: 888
     7 },
     8 {
     9   ......
    10 }

     

     1 // GetUserInfo.js
     2 GetUI(o){
     3 
     4   let uid = Number(o.uid) || [] // 666
     5   let name = o.name || [] // '柚子'
     6   let lv = Number(o.lv) || [] //
     7   let group = o.group || [] //
     8 
     9   // group & lv 为空时
    10   if (group == '' && lv == '') {
    11     db.collection('user')
    12       .where({
    13         uid: _.in(uid), // 筛id
    14         name: _.in(name) // 筛名字
    15       })
    16       .get()
    17       .then(res => {
    18         console.log("返回:", res.data)
    19       })
    20       .catch(err => {
    21         console.log("错误:", err);
    22       })
    23   } else if (group != '' && lv == '') { //group不为空 & lv 为空时
    24     db.collection('user')
    25       .where({
    26         uid: _.in(uid), // 筛id
    27         name: _.in(name), // 筛名
    28         group: _.in(group) // 筛组
    29       })
    30       .get()
    31       .then(res => {
    32         console.log("返回:", res.data)
    33       })
    34       .catch(err => {
    35         console.log("错误:", err);
    36       })
    37   } else if (group == '' && lv != '') { //group为空 & lv 不为空时
    38     db.collection('user')
    39       .where({
    40         uid: _.in(uid), // 筛id
    41         name: _.in(name), // 筛名
    42         lv: _.in(lv) // 筛级
    43       })
    44       .get()
    45       .then(res => {
    46         console.log("返回:", res.data)
    47       })
    48       .catch(err => {
    49         console.log("错误:", err);
    50       })
    51   }
    52 }
    .where{} 参数值为空时,查询不到数据~
      https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/database/dbscript.html
     文档说不支持各种表达式,.......
     我不想写这么多if else......
     有什么办法能不写N个if else???
     感谢~

     


    stop eating 大佬的方法:

     1 let condition = {
     2   uid: _.in(uid), 
     3   name: _.in(name)
     4 };
     5 if(group == '' && lv != '') {
     6    condition.iv = _.in(lv);
     7 }else {
     8   ...
     9 }
    10 11 db.collection('test').where(condition).get()
  • 相关阅读:
    XUL
    weblogic更新license步骤
    用Eclipse+ axis2_1.1.1+tomcat5.5 开发Web Services
    网管和黑客都必须知道的命令
    WebLogic Server实现双向SSL
    配置 WebLogic 9
    JSTL fmt:formatNumber 数字、货币格式化
    关闭myeclipse的Quick Update自动更新功能
    ibatis2.3+mysql5.1+resin3.15乱码感想
    配置MyEclipse 6 自带的tomcat6
  • 原文地址:https://www.cnblogs.com/iblackly/p/13352961.html
Copyright © 2011-2022 走看看