SailsJS,Waterline用select填充记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SailsJS,Waterline用select填充记录相关的知识,希望对你有一定的参考价值。

我看过很多旧的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

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

答案

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

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'])
}

以上是关于SailsJS,Waterline用select填充记录的主要内容,如果未能解决你的问题,请参考以下文章

与你相遇好幸运,async解决循环回调问题

Node中的ORM库-Waterline

Sails.Js - 我如何在sails.Js 中进行分页

与你相遇好幸运,Waterline初遇

Sails.js 1.0:如何在没有Waterline的情况下连接和使用旧版MySQL数据库?

nodejs-ORM 操作数据库中间件waterline的使用