首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SailsJS,Waterline使用select填充记录

SailsJS,Waterline使用select填充记录
EN

Stack Overflow用户
提问于 2019-04-11 00:45:29
回答 2查看 1.7K关注 0票数 3

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

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

代码语言:javascript
复制
await Document.find({id: id}).populate('createdBy', {select: ['name']})

我得到了

代码语言:javascript
复制
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' ] }

在模型中,

代码语言:javascript
复制
createdBy: {
      model: 'user',
      description: 'Who is this document assigned to'
    },

我用的是sails 1.1.0,waterline 0.13.5-0

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

EN

回答 2

Stack Overflow用户

发布于 2019-11-19 21:48:51

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

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

转到此部分

代码语言:javascript
复制
// If this is a singular ("model") association, then it should always have
// an empty dictionary on the RHS.  (For this type of association, there is
// always either exactly one associated record, or none of them.)
if (populateAttrDef.model) {....}

将其更改为:

代码语言:javascript
复制
     if (populateAttrDef.model) {

        // Tolerate a subcriteria of `{}`, interpreting it to mean that there is
        // really no criteria at all, and that we should just use `true` (the
        // default "enabled" value for singular "model" associations.)
        if (_.isEqual(query.populates[populateAttrName], {})) {
          query.populates[populateAttrName] = true;
        }
        // Otherwise, this simply must be `true`.  Otherwise it's invalid.
        else {

          if (query.populates[populateAttrName] !== true && (_.isUndefined(query.populates[populateAttrName].select) && _.isUndefined(query.populates[populateAttrName].omit))) {
            throw buildUsageError(
              'E_INVALID_POPULATES',
              'Could not populate `'+populateAttrName+'` 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!\n'+
              '(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.)\n'+
              '\n'+
              'Here\'s what was passed in:\n'+
              util.inspect(query.populates[populateAttrName], {depth: 5}),
              query.using
            );
          }//-•
          else {
            query.populates[populateAttrName] = {
              select: query.populates[populateAttrName].select? query.populates[populateAttrName].select : undefined,
              omit: query.populates[populateAttrName].omit? query.populates[populateAttrName].omit : undefined
            };
          }

        }//>-•

      }

这是拉取请求,目的是确切地查看您应该更改的内容:

票数 2
EN

Stack Overflow用户

发布于 2019-04-11 14:28:51

当您使用一对一关联时,您不能使用子标准,例如错误。

代码语言:javascript
复制
So passing in subcriteria (i.e. as the second argument to `.populate()`) is not supported for this association

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

代码语言:javascript
复制
customToJSON: function() {

  return _.omit(this, ['createdAt', 'updatedAt', 'id'])
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55617600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档