2015-08-15 40 views
4

根據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創建的迴應嗎?還是必須爲了讓寵物填充而做另一個請求?

+0

你肯定寵物'Gummy' id爲'1'是整數? –

+1

這是一個自動生成的主列,所以我認爲它應該是。此外,創建後調用'GET/pony'會正確填充記錄。 –

+0

好的,如果你確定'Gummy'存在與ID'1'。請參閱下面的答案。 –

回答

4

默認情況下,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); 
     }); 
    }); 
} 
+0

不錯。非常感謝。所以,文件再一次有點誤導。 –

+0

似乎愚蠢的MVC框架有這樣的水平的麻煩這麼基本... – Sebastialonso

+0

@Sebastialonso我不知道目前的階段,它已經實施核心或沒有,因爲我從來沒有使用Sails了:p –

1

安迪的答案是現貨。下面是使用一個實現水線的承諾

API /藍圖/ 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); 
}