【问题标题】:Create model in model在模型中创建模型
【发布时间】:2014-12-22 08:28:38
【问题描述】:

我是 strongloop 的新手,我会查看文档和示例,但从来没有看到我想要的问题。 我有两个模型,sentence.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。现在,哪个触发方法或任何其他范围正确且最适合解析句子模型和创建日志模型?

谢谢!

【问题讨论】:

    标签: node.js rest loopbackjs strongloop


    【解决方案1】:

    在你的情况下,最好的地方是

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

    Model hook Sentence.afterInitializeModel Event Sentence.on('set') 也可用,但在某些额外情况下会调用。

    (请注意,在我的情况下,我将使用远程挂钩并且仅使用 ONE Log 模型。)

    【讨论】:

    • 谢谢,那么在这个范围内创建和保存 log.js 模型怎么样?
    • 是的,你可以做到,通过Sentence.app.models.Logrequire('../../server/server.js').models.Logrequire('loopback').getModel('Log')访问Log模型。在我的项目中,我将钩子放在单独的文件中,然后使用require('../hooks/sentence.js')(server) 将它们插入到 server/boot/root.js 中。所以在 sentence.js 中,我引用了这样的模型:server.models.Log.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多