风帆蓝图填充不适用于创作?
Sails blueprint populate not working on creation?
根据 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
创建,或者我必须做另一个请求只是为了让宠物填充?
默认情况下,blueprint
在 create
操作不会通过创建的任何新实例执行 populateAll
。看看是source code.
如果您想自动填充创建的内容,您应该在 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);
});
});
}
安迪的回答很到位。这是一个使用水线承诺的实现
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);
}
根据 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
创建,或者我必须做另一个请求只是为了让宠物填充?
默认情况下,blueprint
在 create
操作不会通过创建的任何新实例执行 populateAll
。看看是source code.
如果您想自动填充创建的内容,您应该在 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);
});
});
}
安迪的回答很到位。这是一个使用水线承诺的实现
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);
}