我看过很多旧的 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'
},
我使用的是 sails 1.1.0
,水线0.13.5-0
我这样做对吗?有办法做到这一点吗?
最佳答案
我解决了问题并提出了拉取请求。由于拉取请求尚未被接受,请小心并根据您的要求使用它。
前往
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
关于orm - SailsJS、Waterline 使用 select 填充记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55617600/