2015-07-02 1 views
2

общей/модель/event.jsonКак получить параметры запроса в модели в Loopback

{ 
"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(); 
}); 
} 

в Access крючках Операции в event.js Я хочу чтобы получить organizer_id параметр из URL определяется в event.json как

"http": { 
    "path": "organizer/:organizer_id/events" 
} 

в примере URL выглядеть example.com/organizer/54c88f62e4b0b0fca2d0f827/events/

Как это сделать?

ответ

0

Вы пробовали loopback.getCurrentContext()?

Вы должны включить его в config.json или config.ENV.js:

"remoting": { 
    "context": { 
    "enableHttpContext": true 
    }, 
    ... 
} 

И тогда она должна быть доступна в вашем крючке.

См http://docs.strongloop.com/display/JA/Using+current+context

Примечание: remoting.context вариант был удален в версии 3.0. См. http://loopback.io/doc/en/lb2/Using-current-context.html для получения более подробной информации.