公共/模型/ event.json
{
"name": "Event",
"mongodb": {
"collection": "event"
},
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"http": {
"path": "organizer/:organizer_id/events"
},
"properties": {},
"validations": [],
"relations": {},
"acls": [],
"methods": []
}
公共/模型/ event.js
module.exports = function (Event) {
Event.disableRemoteMethod('upsert', true);
Event.disableRemoteMethod('exists', true);
Event.disableRemoteMethod('findOne', true);
Event.disableRemoteMethod('count', true);
Event.disableRemoteMethod('prototype.updateAttributes', true);
// Before get records put orgnaizerId in query
Event.observe('access', function (ctx, next) {
var organizer_id = '';
if (ctx.query.where) {
ctx.query.where.organizerId = organizer_id;
} else {
ctx.query.where = {organizerId: organizer_id};
}
console.log(ctx.query);
next();
});
}
在event.js中的Access Operation挂钩中的我想从URL获取的organizer_id参数在event.json中定义如
"http": {
"path": "organizer/:organizer_id/events"
}
示例网址中的看起来像 example.com/organizer/54c88f62e4b0b0fca2d0f827/events /
怎么做?
答案 0 :(得分:0)
您是否尝试过loopback.getCurrentContext()
?
您必须在config.json或config.ENV.js中启用它:
"remoting": {
"context": {
"enableHttpContext": true
},
...
}
然后它应该在你的钩子里可用。
请参阅http://docs.strongloop.com/display/JA/Using+current+context
注意:3.0版中 remoting.context 选项已删除。有关详细信息,请参阅http://loopback.io/doc/en/lb2/Using-current-context.html。