Jahid Shohel
Jahid Shohel

Reputation: 1485

Return from a beforeRemote

I have two questions regarding "beforeRemote" method of loopback-

  1. How do I get hold of model methods inside beforeRemote method? I mean inside beforeRemote I want to invoke (lets say) "upsert" method of the model.
  2. How do I return invocation from beforeRemote? By return I mean instead of hitting the target invoked method the execution will return from beforeRemote method.

My code -

Installation.beforeRemote("create", function (context, result, next) {
    var data = context.req.body;
    console.log("ImEI" + data.imei);
    data.vendor = "jahid";

    var self = this;
    var filter = {where: {imei: data.imei}};

    //the self here point to global object. but i want self to point to model
    self.findOne(filter, function (err, result) {
        if (result) {
            data.id = result.id;
            self.upsert(data, function(err, result){
            if(err){
                next(err);
            } else if(result) {
        //here i want to send a valid response back to client with 200 and body as my model.
                next(data);
            }
        });
        return;
    }
    next();
});
});

Upvotes: 0

Views: 749

Answers (1)

superkhau
superkhau

Reputation: 2781

  1. You have access to the Installation model from the module.exports declaration:

    module.exports = function(Installation) {
      ...
      Installation.upsert...
      ...
    }
    
  2. You have access to the response object from the context object. So you could just respond with something like context.res.send('hello world') and not call next().

Upvotes: 2

Related Questions