我查看了很多旧的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
我这样做正确吗?有办法吗?
答案 0 :(得分:1)
当您使用一对一关联时,您不能使用诸如错误提示之类的子标准。
So passing in subcriteria (i.e. as the second argument to `.populate()`) is not supported for this association
U可以在模型customToJSON
上使用createdBy
函数来省略数据。
customToJSON: function() {
return _.omit(this, ['createdAt', 'updatedAt', 'id'])
}
答案 1 :(得分:1)
我解决了问题,并提出了要求。由于请求请求尚未被接受,请当心并根据您的要求使用它。
转到
node_modules / waterline / lib / waterline / utils / query / forge-stage-two-query.js
转到本节
// 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) {....}
将其更改为:
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
};
}
}//>-•
}
这是拉取请求,用于准确查看您应更改的内容: https://github.com/balderdashy/waterline/pull/1613