【问题标题】:Sails blueprint populate not working on creation?Sails 蓝图填充不工作?
【发布时间】:2016-02-01 12:55:00
【问题描述】:

根据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 的回复。

我的步骤是这样的:

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

我应该做任何其他事情来让蓝图 api 填充 pet 属性以响应 pony 创建,还是我必须做另一个请求才能让宠物填充?

【问题讨论】:

  • 你确定宠物Gummy id 是1 是整数吗?
  • 这是一个自动生成的主列,所以我认为应该是。此外,在创建后调用 GET /pony 会返回正确填充的记录。
  • 好吧,如果您确定Gummy 存在,ID 为1。请参阅下面的答案。

标签: sails.js


【解决方案1】:

默认情况下,blueprint create 操作不会通过创建的任何新实例执行 populateAll。看是source code

如果您想自动填充创建的内容,您应该覆盖默认的 blueprint create 操作,类似于。

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);
      });
  });
}

【讨论】:

  • 不错。非常感谢。因此,文档再次有点误导。
  • 对于一个 MVC 框架来说,对于如此基本的东西有这种程度的麻烦似乎很愚蠢......
  • @Sebastialonso 我不知道当前阶段,它是否已经在核心上实现了,因为我不再使用 Sails:p
【解决方案2】:

安迪的回答很到位。这是一个使用水线承诺的实现

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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多