Search code examples
loopbackjsstrongloop

Loopback get http context in an overriden built-in method


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?


Solution

  • 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!