【问题标题】:how to pass data from one model into another loopback如何将数据从一个模型传递到另一个环回
【发布时间】:2018-03-21 17:57:13
【问题描述】:

伙计们,我是环回新手,所以不知道该怎么做。这是我的代码

 module.exports = function(Customer) {

    Customer.beforeRemote('create', function (ctx, user, next) {
    var companyprofile=app.models.customerdetail
    companyprofile.create(ctx.args.data.cust_cmp_profile,user,next)// is this possible to pass selected value to model customerdetail
    console.log(ctx);
    });
}; 

这两个模型是不相关的,所以我在这里在 Customer 中调用 customerdetail 模型,我尝试使用 customerdetail 的 create 方法,但不知道该怎么做。我搜索了很多,但没有任何东西我怎么能做到这一点

【问题讨论】:

  • 我相信你想要这里的“处理数据”文档:loopback.io/doc/en/lb3/Working-with-data.html。这描述了如何通过服务器端 API 对数据进行 CRUD。
  • @RaymondCamden 我在客户模型中调用customerdetail ..您提供的链接没有提供任何示例。你能给我任何与我的问题相关的例子吗?谢谢你的帮助

标签: loopbackjs strongloop


【解决方案1】:

试试这个希望对你有帮助

    Customer.beforeRemote('create', function (ctx, user, next) {

   var companyprofile=app.models.customerdetail
   companyprofile.create({cust_cmp_profile:ctx.args.data.cust_cmp_profile},              function(err) {
    if (err){
        next(err)
     } 
    else{
        next()
    }
 });   
});

【讨论】:

  • 即使我想将数据从一个模块传递到另一个模块。所以我根据我的要求添加了上述内容,但它会引发错误,例如未定义公司简介。。代替公司简介,我使用了我的模块名称。
  • @lakshmipriya var companyprofile=app.models.customerdetail 。 “customerdetail”是您的模型名称检查您在 app.models 之后提供的名称,它应该与特别给出的名称完全相同(您的模型)json 文件
  • 你能回答这个stackoverflow.com/questions/55568366/…吗,因为我想在另一个模块中获取附加的文件名
  • 我想将附件模块的文件名传递给职业模块
【解决方案2】:

要在您的模型中使用app,您应该执行以下操作:

module.exports = function(Customer) {
  let app
  
  // Your code like app.models
  
  Customer.on('attached', function(a) {
    app = a
  })
}

【讨论】:

  • 以上代码也运行良好,无论如何感谢您的建议
猜你喜欢
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-09
  • 1970-01-01
  • 1970-01-01
  • 2016-06-18
  • 2019-10-15
相关资源
最近更新 更多