Orm 帆船、水线用select填充记录

Orm 帆船、水线用select填充记录,orm,sails.js,waterline,Orm,Sails.js,Waterline,我已经研究了很多旧的SO问题,这些问题破坏了GitHub链接和SailsJS Trello,但是我仍然不清楚 是否可以在SailsJS中填充字段(一对一关系),并仅返回特定字段(通过选择或忽略) 我越来越 UsageError: Invalid populate(s). Details: Could not populate `createdBy` because of ambiguous usage. This is a singular ("model") association, w

我已经研究了很多旧的SO问题,这些问题破坏了GitHub链接和SailsJS Trello,但是我仍然不清楚

是否可以在SailsJS中填充字段(一对一关系),并仅返回特定字段(通过选择或忽略)

我越来越

UsageError: Invalid populate(s).
Details:
  Could not populate `createdBy` because of ambiguous usage.  This is a singular ("model") association, which means it never refers to more than _one_ associated record.  So passing in subcriteria (i.e. as the second argument to `.populate()`) is not supported for this association
, since it generally wouldn't make any sense.  But that's the trouble-- it looks like some sort of a subcriteria (or something) _was_ provided!
(Note that subcriterias consisting ONLY of `omit` or `select` are a special case that _does_ make sense.  This usage will be supported in a future version of Waterline.)

Here's what was passed in:
{ select: [ 'name' ] }

在模型中

createdBy: {
      model: 'user',
      description: 'Who is this document assigned to'
    },
我使用的是帆船
1.1.0
,水线
0.13.5-0


我这样做对吗?有什么方法可以做到这一点吗?

当你使用一对一关联时,你不能使用次标准,比如说错误

So passing in subcriteria (i.e. as the second argument to `.populate()`) is not supported for this association
您可以对模型
createdBy
使用
customToJSON
函数来省略数据

customToJSON: function() {

  return _.omit(this, ['createdAt', 'updatedAt', 'id'])
}

我解决了这个问题并提出了一个请求。由于拉动请求尚未被接受,请小心并在您的请求中使用它

node_modules/waterline/lib/waterline/utils/query/forge-stage-two-query.js

转到本节

//如果这是一个单一的(“模型”)关联,那么它应该始终具有
//右边是一本空字典。(对于这种类型的关联,有
//始终只有一条关联记录,或者没有关联记录。)
if(populateAttrDef.model){….}
将其更改为:

if(populateAttrDef.model){
//容忍次标准`{}`,将其解释为
//实际上根本没有任何标准,我们应该只使用'true'(the
//单一“模型”关联的默认“启用”值。)
if(u.isEqual(query.populates[populateAttrName],{})){
query.populates[populateAttrName]=true;
}
//否则,该值必须为“true”,否则无效。
否则{
if(query.populates[populateAttrName]!==true&&(551;未定义(query.populates[populateAttrName].select)&&&&&551;未定义(query.populates[populateAttrName].省略))){
抛出buildUsageError(
“E_无效_填充”,
'由于用法不明确,无法填充''+populateAttrName+''+
“这是一个单一的(“模型”)关联,这意味着它从未提及”+
'多个相关记录。因此传入次标准(即as'+
'此关联不支持'.populate()`'的第二个参数,'+
因为它通常没有任何意义。但这就是问题所在+
'看起来像某种次标准(或什么)\提供了!\n'+
(请注意,仅由'omit'或'select'组成的次标准是特殊的'+
'如果u确实有意义。此用法将在未来版本中受支持'+
'水线的长度。)\n'+
“\n”+
'这是传入的内容:\n'+
util.inspect(query.populates[populateAttrName],{depth:5}),
查询.使用
);
}//-•
否则{
query.populates[populateAttrName]={
选择:查询。填充[populateAttrName]。选择?查询。填充[populateAttrName]。选择:未定义,
忽略:查询。填充[populateAttrName]。忽略?查询。填充[populateAttrName]。忽略:未定义
};
}
}//>-•
}
这是一个pull请求,用于查看您应该更改的内容:

当使用find()函数时,cutomToJson函数会启动吗?我已经将它添加到文档和用户模型中,但是它根本不会被触发。事实上,甚至User.findOne({id:id})也会返回完整的记录,包括省略的字段。函数设置好了吗<代码>{attributes:{},customToJSON:function(){return{.omit(this,['createdAt','updatedAt','id'])}是的,这正是它的设置方式(使用包含字段的
属性
)。我一直在尝试调试它,但没有使用customToJSON方法,整个记录将从数据库返回,但是当从控制器返回一些JSON响应时(即
exits.success({records:theReturnedRecords})
它应该在发送HTTP响应之前运行。这对于避免意外分发用户密码之类的事情来说是非常有用的,但这里可能没有这么多。正如Sails/Waterline生成的关于您最初使用
select
的警告所述:“Waterline的未来版本将支持此用法。”
customToJSON:function
不要忽略数据库中的记录,当模型使用JSON.stringify()时,它将触发,请查看文档。因此,当您使用res.JSON(数据)时,res.send()或如果您使用actions2存在。success(数据),它将触发function
.toJSON()
此函数使用
customToJSON
setup。您是否看到响应中省略的字段?
customToJSON: function() {

  return _.omit(this, ['createdAt', 'updatedAt', 'id'])
}