0

My goal is to send some request after certain model was created, updated or deleted. So I implemented the following:

module.exports = function(TerminalUser) {

var http = require('http'); 

var tsReloadUser = function(id) {

    var options = {                     
        auth: 'user:pwd',
        host: 'host.domain.local',
        port: 8080,
        path: '/CmdUserCacheRefresh?userid=' + id,
        method: 'GET'
    };

    http.request(options, function(res) {

        console.log('STATUS: ' + res.statusCode);
        console.log('HEADERS: ' + JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            console.log('BODY: ' + chunk);
        }); 

    }).end();
}

TerminalUser.afterSave = function(next) {

    console.log('afterSave');

    tsReloadUser(this.id);

    next();
};

TerminalUser.afterUpdate = function(next) {

    console.log('afterUpdate');

    tsReloadUser(this.id);

    next();
}

TerminalUser.afterDestroy = function(next) {

    console.log('afterDestroy');

    tsReloadUser(this.id);

    next();
};  

};

The hook afterSave is call when an entity was created and everything runs as expected. But for some strange reason neither afterUpdate/Destroy is called nor a error message is thrown when a user updates or delete an entity instance (the data is successfully stored in the database). Can anyone give me a hint whats going on?

ragn
  • 55
  • 1
  • 8

2 Answers2

0

See an example in my new repo: https://github.com/strongloop/loopback-faq-model-hooks

There is an issue with afterDestroy, the issue is waiting to be resolved

superkhau
  • 2,781
  • 18
  • 9
0

Try to implement your logic with changed and deleted Model's Events

IvanZh
  • 2,265
  • 1
  • 18
  • 26