Sails蓝图populate在创建时无法工作?

4
根据此链接,在创建与其他记录关联的新记录时,响应应包含已填充的关联记录。
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 的响应。

我的步骤如下:

  1. sails new test
  2. sails generate api pony
  3. sails generate api pet
  4. 在两个模型中添加 "name" : "string"
  5. 在pony模型中添加 "pet" : { "model" : "pet" }

为了让蓝图API填充响应中的pony属性,我需要做些什么呢?还是说我必须发送另一个请求才能获取填充的pet


你确定宠物“Gummy”的ID是整数“1”吗? - Andi N. Dirgantara
1
这是一个自动生成的主列,所以我认为应该是这样的。此外,在创建后调用 GET /pony 会返回正确填充的记录。 - František Žiačik
好的,如果你确定Gummy存在且ID为1。请看下面我的回答。 - Andi N. Dirgantara
2个回答

4
默认情况下,create操作中的blueprint不会通过任何新创建的实例执行populateAll。请查看源代码
如果您想自动填充已创建的内容,应该覆盖默认的create操作中的blueprint,例如:
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);
      });
  });
}

太棒了。非常感谢你。所以,文件说明再次有点误导。 - František Žiačik
一个MVC框架为了如此基础的东西而有这么多麻烦,看起来很傻。 - Sebastialonso
@Sebastialonso 我不知道当前阶段它是否已经在核心上实现了,因为我不再使用Sails了 :p - Andi N. Dirgantara

1
安迪的回答完全正确。这是使用Waterline的承诺实现的示例。

api/blueprints/create.js

'use strict';

let actionUtil = require('sails/lib/hooks/blueprints/actionUtil')

/**
 * Create Record
 *
 * post /:modelIdentity
 *
 * An API call to find and return a single model instance from the data adapter
 * using the specified criteria.  If an id was specified, just the instance with
 * that unique id will be returned.
 *
 * Optional:
 * @param {String} callback - default jsonp callback param (i.e. the name of the js function returned)
 * @param {*} * - other params will be used as `values` in the create
 */
module.exports = function createRecord (req, res) {
  var Model = actionUtil.parseModel(req);

  var data = actionUtil.parseValues(req);

  Model
    .create(_.omit(data, 'id'))
    .then(newInstance => Model.findOne(newInstance.id).populateAll())
    .then(res.created)
    .catch(res.negotiate);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接