Create model in model

若如初见. 提交于 2019-12-11 12:14:23

问题


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



来源:https://stackoverflow.com/questions/27599031/create-model-in-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!