1

I am talking about loopback push component. I am trying to intercept the "create" method of "Installation" model. My code looks like this -

server/boot/installationex.js

module.exports = function (app) {
var Installation = app.models.Installation;
var create = Installation.create;

Installation.create = function (data, cb) {
   //reinitializing old implementation
   this.create = create;
   console.log("Received data: "+JSON.stringify(data));

    if (!data || !data.imei) {
       console.log("No data or imei was provided, creating new");
       this.create(data, cb);
       return;
   }

    //saving 'this' reference

    var that = this;
   //search by imei filter
   var filter = {where: {imei: data.imei}};
   this.findOne(filter, function (err, result) {
       if (err) {
           console.log("Error occurred while looking for installation by IMEI");
           cb(err);
           return;
       }
       if (!result) {
           console.log("No installation found by IMEI, will create a new installation");
           that.create(data, cb);
           return;
       }

        console.log("Found existing installation with id: " + JSON.stringify(result));

        result.deviceToken = result.gpsLocation = result.osVersion = result.vendor = result.phoneNumbers = null;

        if (data.deviceToken) {
           result.deviceToken = data.deviceToken;
       }
       if (data.gpsLocation) {
           result.gpsLocation = data.gpsLocation;
       }
       if (data.osVersion) {
           result.osVersion = data.osVersion;
       }
       if (data.vendor) {
           //result.vendor=data.vendor;
           result.vendor = 'jahid';
       }
       if (data.phoneNumbers) {
           result.phoneNumbers = data.phoneNumbers;
       }
       that.upsert(result, cb);
   });
  }
}

Unfortunately this code is invoked only once, I mean the first time. After that this code is never invoked. I became sure by looking at the log. It only prints the log first time. After that it does not print any log.

Any idea why this glue code is only invoked once? My intention is to intercept all create method invocation for Installation model. And check if there is already an entry for supplied "IMEI", if so then reuse that. Otherwise create new.

Thanks in advance.

Best regards,

Jahid

Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
Jahid Shohel
  • 1,395
  • 4
  • 18
  • 34

2 Answers2

2

What I would start here with is:

  1. instead of implementing your own intercepting mechanism use Model Hooks
  2. check out findOrCreate() method
Alex V
  • 1,155
  • 8
  • 10
  • Any example how can i call "findOrCreate()" from Android side? I mean not the server side, but I need client side code. – Jahid Shohel Nov 23 '14 at 10:01
0

boot scripts are only run once during application startup. if you want a function that triggers every time a function is called, use a remote hook or model hook. probably something along the lines of:

...
Installation.beforeRemote('create', ...
...

see http://docs.strongloop.com/display/LB/Adding+logic+to+models for more info

superkhau
  • 2,781
  • 18
  • 9
  • Hi,according to this link http://docs.strongloop.com/display/public/LB/Customizing+models (At the bottom of the page), that is the suggested way to overload a builtin method. – Jahid Shohel Nov 23 '14 at 09:56