Javascript 如何在环回中获取模型内的请求参数
common/models/event.jsonJavascript 如何在环回中获取模型内的请求参数,javascript,json,node.js,express,loopbackjs,Javascript,Json,Node.js,Express,Loopbackjs,common/models/event.json { "name": "Event", "mongodb": { "collection": "event" }, "base": "PersistedModel", "idInjection": true, "options": { "validateUpsert": true }, "http": { "path": "organizer/:organizer_id/events" }, "properties": {}
{
"name": "Event",
"mongodb": {
"collection": "event"
},
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"http": {
"path": "organizer/:organizer_id/events"
},
"properties": {},
"validations": [],
"relations": {},
"acls": [],
"methods": []
}
通用/models/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操作钩子中,我想从event.json中定义的URL中获取organizer_id参数
"http": {
"path": "organizer/:organizer_id/events"
}
在示例中,URL看起来像
example.com/organizer/54c88f62e4b0b0fca2d0f827/events/
如何执行此操作?您是否尝试过
环回。getCurrentContext()
您必须在config.json或config.ENV.js中启用它:
"remoting": {
"context": {
"enableHttpContext": true
},
...
}
然后它应该在你的钩子里
看
注意:remoting.context选项在3.0版中被删除。有关更多详细信息,请参阅。类似的内容在使用环回上下文模块时对我有效:const\u ctx=LoopBackContext.getCurrentContext()const method=\u ctx.get('http').req.method