在模型中创建模型 [英] Create model in model
本文介绍了在模型中创建模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Strongloop的新手,正在查看文档和示例,但从未看到所需的问题. 我有两种模型,例如sence.js和log.js,并让post请求从移动应用到rest-api的句子
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
模型已触发方法,例如afterInitialize,beforeValidate和beforeSave.现在,哪个触发方法或任何其他作用域最正确,最适合解析句子模型和创建日志模型?
谢谢!
解决方案
您认为最好的地方是
Sentence.beforeRemote('create', function(ctx, sentence, next){
console.log(ctx.req.body);
next()
})
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.)
这篇关于在模型中创建模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文