[英]Moongose find with or without a condition
我有一個node.js / mongodb應用程序的簡單問題。 如果用戶是超級管理員,而我是普通管理員,則我希望獲得所有用戶。我只需要同一客戶端的用戶。
我已經嘗試過這樣的事情:
const client = req.user.role === 'SA_ROLE'? null : req.user.client;
User.find({'client': client})...
並且:
const client = req.user.role === 'SA_ROLE'? '': req.user.client;
User.find({'client': client})...
但是在兩種情況下,當用戶為SA時,它都會返回Cero用戶。
如果要獲取所有用戶,則過濾器應為{}
。 因此,此代碼可能有效:
let query = req.user.role === 'SA_ROLE'? {} : {client: req.user.client};
User.find(query)...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.