【问题标题】:Save multiple models in loopback在环回中保存多个模型
【发布时间】:2016-03-24 06:45:37
【问题描述】:

我正在研究环回,想知道是否可以从一个请求中保存到多个模型。说..一个帖子有很多标签和很多图片。用户表单将具有以下内容:

  • 帖子标题
  • 帖子说明
  • 标签名称(一个多字段。例如:['Sci-Fi', 'Fiction', 'BestSeller']
  • 图像文件(希望处理上传到 AWS 的文件,可能使用 skipper-s3?)

我如何能够坚持使用这样的多个模型?这是你用钩子做的事情吗?

【问题讨论】:

    标签: javascript loopbackjs strongloop


    【解决方案1】:

    您可以在模型中创建RemoteMethods,该模型可以定义参数,因此在您的示例中,您可以在 Post 模型中创建类似这样的内容:

    // remote method, defined below
    Post.SaveFull = function(title, 
            description,
            tags,
            imageUrl,
            cb) {
    
        var models = Post.app.Models; // provides access to your other models, like 'Tags'
    
        Post.create({"title": title, "description": description}, function(createdPost) {
             foreach(tag in tags) {
                 // do something with models.Tags
    
             }
             // do something with the image
    
             // callback at the end
             cb(null, {}); // whatever you want to return
        })
    
    }
    
    Post.remoteMethod(
        'SaveFull', 
        {
          accepts: [
              {arg: 'title', type: 'string'},
              {arg: 'description', type: 'string'},
              {arg: 'tags', type: 'object'},
              {arg: 'imageUrl', type: 'string'}
            ],
          returns: {arg: 'Post', type: 'object'}
        }
    );
    

    【讨论】:

    • 你是最棒的!我刚刚注意到这一点,并且我正在玩它,但这对我来说非常清楚地证实了这一点。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多