首先想问一下unicloud-db组件查询数据是什么时候就触发的?
首先在unicloud-db组件where属性设置为::where="goWhere()"。组件加载方式为manual,在scrip部分的onready()内手动加载数据this.$refs.udb.loadData()
然后goWhere()具体见下。实现功能为判断当前登录账号角色,按照角色返回不同的查询条件,对于employee角色,在后边拼接一个变量this.reserve_objID,该变量从数据库中查询出结果后拼接到字符串后边,用于筛选仅针对某个employee用户的reserve记录。
goWhere(){
db.collection('uni-id-users').where('_id==$cloudEnv_uid').field('username').get().then(res=>{
this.reserve_objID=res.result.data[0].username
if(this.role.length>0&&this.role.indexOf('employee')>-1){
console.log(this.reserve_objID)
console.log('reserve_state[0].state_num==1'+'&&reserve_objID=="'+this.reserve_objID+'"')
return this.where='reserve_state[0].state_num==1'+'&&reserve_objID=="'+this.reserve_objID+'"'
}
else if(this.role.length>0&&this.role.indexOf('visitor_admin')>-1){
console.log('visitor_admin')
return this.where='reserve_state[0].state_num==2'
}
else if(this.role.length>0&&this.role.indexOf('admin')>-1){
console.log('admin')
return this.where='reserve_state[0].state_num==1'
}
else{
return this.where='reserve_state[0].state_num==0'
}
})
我在goWhere()内console两个变量用于监视goWhere()函数运行状态,发现在控制台go Where()运行了两次,但是并没有成功筛选出数据(当不拼接这个变量函数直接返回完整字符串时可以成功筛选,但是因为业务需要条件字符串必须要包含一个变量的)。怀疑是不是渲染页面的时候还没等reserve_objID从数据库查询到就执行了一次unicloud-db。
控制台输出结果:
1***@qq.com (作者)
好的好的,感谢!
2021-10-04 16:56
DCloud_uniCloud_JSON
回复 1***@qq.com: 另外推荐你看一下视频教程:https://www.bilibili.com/video/BV17p4y1a71x?p=9
2021-10-04 17:17