Save multiple models in loopback

前端 未结 1 612
梦毁少年i
梦毁少年i 2021-01-07 13:23

I\'m doing research on loopback and was wondering if it\'s possible to save to multiple models from one request. Say.. an Post has many Tags with many Images. A user form wo

相关标签:
1条回答
  • 2021-01-07 13:57

    You can create RemoteMethods in a Model, which can define parameters, so in your example you could create something like this in your Post model:

    // 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'}
        }
    );
    
    0 讨论(0)
提交回复
热议问题