gpt4 book ai didi

sails.js - Sails 蓝图填充不工作?

转载 作者:行者123 更新时间:2023-12-05 00:58:44 24 4
gpt4 key购买 nike

根据 this , 在创建与其他记录关联的新记录时,响应应包含填充的关联记录。

POST /pony
{
"name": "Pinkie Pie",
"pet": 1
}

响应应该是这样的
{
"name": "Pinkie Pie",
"pet": {
"name": "Gummy",
"id": 1
},
"id": 4,
"createdAt": "2013-10-18T01:22:56.000Z",
"updatedAt": "2013-11-26T22:54:19.951Z"
}

相反,我实际上得到了一个 "pet": 1作为回应。

我的步骤是这样的:
  • sails new test
  • sails generate api pony
  • sails generate api pet
  • 添加 "name" : "string"两种型号
  • 添加 "pet" : { "model" : "pet" }小马模型

  • 我应该做些什么来让蓝图 api 填充 pet属性响应 pony创建还是我必须做另一个请求才能填充宠物?

    最佳答案

    默认情况下,blueprintcreate行动不做populateAll通过创建的任何新实例。看它是source code .

    如果你想自动填充创建的内容,你应该覆盖默认 blueprintcreate行动,类似的东西。

    create: function(req, res){
    var Model = actionUtil.parseModel(req);

    // Create data object (monolithic combination of all parameters)
    // Omit the blacklisted params (like JSONP callback param, etc.)
    var data = actionUtil.parseValues(req);


    // Create new instance of model using data from params
    Model.create(data).exec(function created (err, newInstance) {

    // Differentiate between waterline-originated validation errors
    // and serious underlying issues. Respond with badRequest if a
    // validation error is encountered, w/ validation info.
    if (err) return res.negotiate(err);

    // If we have the pubsub hook, use the model class's publish method
    // to notify all subscribers about the created item
    if (req._sails.hooks.pubsub) {
    if (req.isSocket) {
    Model.subscribe(req, newInstance);
    Model.introduce(newInstance);
    }
    Model.publishCreate(newInstance, !req.options.mirror && req);
    }

    // Send JSONP-friendly response if it's supported
    // populate it first
    Model
    .findOne({id:newInstance.id})
    .populateAll()
    .exec(function(err, populatedInstance){
    if (err) return res.negotiate(err);

    res.created(populatedInstance);
    });
    });
    }

    关于sails.js - Sails 蓝图填充不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32029709/

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