1

I want to use a custom endpoint for POST /images; so what i thought i could override the create method of a model; this is how I'm doing it:

var loopback = require('loopback');


function overrideImageApiMethods(app){
    var Image = app.models.Image,
        create = Image.create;

    Image.create = function create(data, clb){
       var context = loopback.getCurrentContext();
    };
 }

module.exports = overrideImageApiMethods;

I would like to get the response object like you would do it in express; I found the getCurrentContext method returns null in the example above.

What is the correct way to about it?

ppoliani
  • 4,792
  • 3
  • 34
  • 62

2 Answers2

1

loopback.getCurrentContext() - returns not exactly what you assume. Its the per request storage - the wrapper for continuation-local-storage. But for me it returns null too.

So to access context with its req & res, you should dig deeper to context implementation or use domains!

Community
  • 1
  • 1
IvanZh
  • 2,265
  • 1
  • 18
  • 26
-1

See http://docs.strongloop.com/display/public/LB/Customizing+models#Customizingmodels-Changetheimplementationofbuilt-inmethods

var Image = app.models.Image;
...
Image.create = function(...) {
  //your logic     
};
superkhau
  • 2,781
  • 18
  • 9
  • That's not the question. I need to get the http context – ppoliani Dec 22 '14 at 20:51
  • If that doesn't work for you, then you should create a remote method with the same name. ... Image.create = function() { ... } ... Image.remoteMethod('create', function() { ... }) ... – superkhau Dec 22 '14 at 21:53
  • See https://groups.google.com/forum/#!searchin/loopbackjs/context$20object$20override/loopbackjs/qo-WuE9ways/igKfmxdrTGEJ – superkhau Dec 22 '14 at 21:53
  • that's what i ended up doing; I thought there is nicer way – ppoliani Dec 22 '14 at 22:19