1

I'm new to strongloop and look at documentation and samples but never see my problem as desired. I'v two models, sentence.js and log.js and making post request a sentence from mobile app to rest-api, for example

Model sentence.js (dont want save to db this model, only for parsing and creating log model)
{
  name: 'sentence',
  type: 'string'
}

Model  log.js
{ name: 'lat', type: 'string' },
{ name: 'lng', type: 'string' }

[HTTP POST]  myserver/api/sentence?d=$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A

A model has triggered methods, e.g, afterInitialize, beforeValidate , beforeSave. Now, which triggered method or any other scope correct and best for parsing sentence model and creating log model ?

Thanks !

blackkara
  • 4,900
  • 4
  • 28
  • 58

1 Answers1

2

In your case the best place is

Sentence.beforeRemote('create', function(ctx, sentence, next){
  console.log(ctx.req.body); 
  next()
})

Also Model hook Sentence.afterInitialize and Model Event Sentence.on('set') are available, but will be called in some extra cases.

(Note that in my case I would use remote hooks and only ONE Log model.)

IvanZh
  • 2,265
  • 1
  • 18
  • 26
  • Thanks, so what about creating and saving log.js model in this scope ? – blackkara Dec 22 '14 at 12:18
  • 1
    Yes you can do it, accessing Log model through `Sentence.app.models.Log` or `require('../../server/server.js').models.Log` or `require('loopback').getModel('Log')`. In my project I place hooks in separate files and plug in them in server/boot/root.js, using `require('../hooks/sentence.js')(server)`. So inside sentence.js I have a reference to models like so: `server.models.Log`. – IvanZh Dec 22 '14 at 14:41