昨晚的原帖: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()