Create model in model

2019-08-09 10:17发布

问题:

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 !

回答1:

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.)