SailsJS,Waterline用select填充记录

问题描述 投票:0回答:1

我看过很多旧的SO问题,这些问题打破了GitHub链接和SailsJS Trello,但我还不清楚。

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

await Document.find({id: id}).populate('createdBy', {select: ['name']})

我正进入(状态

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

我这样做了吗?有没有办法做到这一点?

orm sails.js waterline
1个回答
1
投票

当你使用一对一关联时,你不能像使用错误那样使用子标准。

So passing in subcriteria (i.e. as the second argument to `.populate()`) is not supported for this association

你可以在模型customToJSON上使用createdBy函数来省略数据。

customToJSON: function() {

  return _.omit(this, ['createdAt', 'updatedAt', 'id'])
}
© www.soinside.com 2019 - 2024. All rights reserved.