Search code examples
node.jsloopbackjs

LOOPBACK Modify another model inside a hook


I want to insert new data in Event model when the property "code" in Transaction model is equal to 3

 module.exports = function(Transaction) {
    Transaction.observe('before save', function(ctx, next) {
        if(ctx.data.code == 3){
          //How to access and insert data in Event model ?
        }
    });
};

Solution

  • If you have an Event model with a function to insert newData. Just reference the model. For ex:

    var eventModel = require('pathtoEventModel');  
    
    module.exports = function(Transaction) {
    Transaction.observe('before save', function(ctx, next) {
        if(ctx.data.code == 3){
           eventModel.saveNewData(ctxBeingNewData,function(response,next){
           //manipulate as per your wish.
           })
        }
    

    }); };