简体   繁体   中英

Loopback operation hook not triggering

I have set an operation hook on my sales table, but the operation not triggering when a new row insert in that table. My hook coke has given below. The database is MySQL.

module.exports = function (LiveSales) {
  LiveSales.observe('after save', function (ctx, next) {
    var socket = LiveSales.app.io;
    console.log("New Item added");
    if (ctx.isNewInstance) {
    } else {
    }
    //Calling the next middleware..
    next();
  }); //after save..
}

Maybe using after remote method.

  LiveSales.afterRemote('*.save', function(ctx, user, next) {
     var socket = LiveSales.app.io;
     console.log("New Item added");

     if (ctx.isNewInstance) {

     } else {

     }
     //Calling the next middleware..
     next();
   }); //after save..
  });

from loopback docs

It depends on how you are inserting data ! If you are inserting via API URL /api/examples/create

You should create a After Remote method like below : Howerver you will not have this option ctx.isNewInstance context will contain these objects req , res , args , result check context objects here .

Below user will be the after remote result or ctx.result .

The MEHTOD can be : create for post or patchOrCreate for patch/upsert method

LiveSales.afterRemote(METHOD, function(ctx, user, next) {
     var socket = LiveSales.app.io;
     console.log("New Item added");

     if (user) {

      } else {

      }

     //Calling the next middleware..
     next();
   });
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM