我遇到問題我無法在Loopback的文檔中找到答案。在beforeRemote遠程掛鉤內添加過濾器
假設我有一個型號Company
和一個型號Employee
。 Company
與其Employees
之間有1Xn的關係。當調用/api/Employees
時,服務器返回所有員工。
我只想返回與請求列表的用戶處於同一公司的員工列表。
對於這一點,我創建了一個遠程鉤
Employee.beforeRemote('find', function(context, modelInstance, next) {
var reject = function() {
process.nextTick(function() {
next(null, false);
});
};
// do not allow anonymous users
var userId = context.req.accessToken.userId;
if (!userId) {
return reject();
}
//I get the details of the user who sent the request
//to learn which company does he belong to
Employee.findById(userId, function(err, user) {
if(!context.req.query.filter) context.req.query.filter={};
context.req.query.filter.where = {brandId:user.companyId};
console.log(context.req.query);
next();
});
});
我想這應該每工作時間,但appearantly它只能當發現已經有一些查詢過濾器等包括 - 雖然執行console.log打印正確的context.req.query對象。
我錯過了什麼?任何幫助將不勝感激!
你是說,當沒有過濾器傳遞給find()時沒有調用'beforeRemote()'鉤子函數,或者你的代碼明確地不工作沒有提供過濾器? – jakerella 2015-03-31 17:30:51
問題在於context.req.query.filter在未從客戶端請求中設置時被忽略。亞歷克斯的建議雖然工作,謝謝大家的幫助。 – 2015-04-01 07:04:56