admin管理员组文章数量:1431520
Given that a user can have many accounts and an account can have many users, with an account always having an owner. Is there a better way to write this in Waterline query syntax?
User.findOneByEmailAddress('[email protected]').then(function(user) {
User.findOne(user.id)
.populate('accounts', {owner: user.id})
.then(console.log);
});
I guess I would prefer something like this, if possible:
User.findOneByEmailAddress('[email protected]')
.populate('accounts', {owner: this.id})
.then(console.log);
While in this case I think a double query must always occur, but it would sure make the code easier to read if the populate() could reference the calling party ID.
I understand this example is a bit contrived.
I also tried this:
User.findOneByEmailAddress('[email protected]').then(function(user) {
user.isAccountOwner().then(console.log);
});
In my Model I defined an instance method:
isAccountOwner: function() {
var _this = this;
return new Promise(function(resolve, reject) {
User.findOne(_this.id)
.populate('accounts', {owner: _this.id})
.then(function(user) {
resolve(!! user.accounts.length > 0);
})
.fail(function(err) {
reject(err);
});
});
Given that a user can have many accounts and an account can have many users, with an account always having an owner. Is there a better way to write this in Waterline query syntax?
User.findOneByEmailAddress('[email protected]').then(function(user) {
User.findOne(user.id)
.populate('accounts', {owner: user.id})
.then(console.log);
});
I guess I would prefer something like this, if possible:
User.findOneByEmailAddress('[email protected]')
.populate('accounts', {owner: this.id})
.then(console.log);
While in this case I think a double query must always occur, but it would sure make the code easier to read if the populate() could reference the calling party ID.
I understand this example is a bit contrived.
I also tried this:
User.findOneByEmailAddress('[email protected]').then(function(user) {
user.isAccountOwner().then(console.log);
});
In my Model I defined an instance method:
isAccountOwner: function() {
var _this = this;
return new Promise(function(resolve, reject) {
User.findOne(_this.id)
.populate('accounts', {owner: _this.id})
.then(function(user) {
resolve(!! user.accounts.length > 0);
})
.fail(function(err) {
reject(err);
});
});
Share
Improve this question
edited Aug 5, 2014 at 3:12
Patrick Wolf
asked Aug 5, 2014 at 0:12
Patrick WolfPatrick Wolf
1,31911 silver badges28 bronze badges
1 Answer
Reset to default 4It took me a few minutes to figure out what you were after, and sadly the answer is no, there's currently no way to access the result of a "parent" query in the populate
criteria. It's an interesting idea that you should consider posting to Github as a feature request. Using this
wouldn't work, it but it could be something like:
User.findOneByEmailAddress('[email protected]')
.populate('accounts', {owner: {parent: 'id'}})
本文标签: javascriptSailsjs Waterline query populateStack Overflow
版权声明:本文标题:javascript - Sails.js Waterline query populate - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745526235a2661847.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论