uhallgarn
uhallgarn

Reputation: 11

loopback after save - data undefined?

I am writing an 'after save' hook and have some problem to read data.

What I would like to do is to access data in another model, but when doing this it is undefined.

Here is some code:

module.exports = function(Fault) {
  Fault.observe('after save', function(ctx, next) {
    ctx.Model.app.models.faulttype.find({where:{id:ctx.instance.faulttypeId}}, function(err, faulttype) {
      if (err)
        console.log(err);
      console.log(faulttype);
      console.log(faulttype.slogan);
  ....
}

I am using the ctx.Model.app.models to grab one of the other models I have (faulttype). The problem is then later when I use it.

In the first console.log it is writing all attributes and values for the faulttype, but when I try to use a specific attribute, as in the second console.log where I usefaulttype.

slogan I get the response that it is undefined. I don't understand why.

Any ideas/comments?

Upvotes: 1

Views: 459

Answers (1)

BlackStork
BlackStork

Reputation: 8331

faulttypeId is reference foregin key in the Fault model?

Since you did not provided enough of information for me to judge, I guessing the problem is that at certain methods there are no ctx.instance - the data is hidden in dynamically assigned property dending on the method : Check this tabel for reference

Could be that the method you using does not pass instance in the context object. Are you using updateAll method?

If you could provide more information (which method, ctx structure (just console.dir(ctx); , etc etc) I could guess more :)

Upvotes: 0

Related Questions