gpt4 book ai didi

javascript - 在环回中保存多个模型

转载 作者:行者123 更新时间:2023-12-02 15:17:22 24 4
gpt4 key购买 nike

我正在研究环回,想知道是否可以通过一个请求保存到多个模型。比如说..一篇文章有​​很多标签和很多图像。用户表单将具有以下内容:

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

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

最佳答案

您可以创建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'}
}
);

关于javascript - 在环回中保存多个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34340692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com