在Sails JS中,父类与子类之一的选择不起作用。相反,我的select语句没有产生结果。所以,寻找最佳答案
模型1:User
module.exports = {
attributes: {
name:{
type:"string",
required:true,
minLength: 3
},
age:{
type:"int",
required:true,
unique: false
},
pets : {
collection : 'pet',
via : 'owners',
dominant: true
}
}
};
模型2:Pet
module.exports = {
attributes: {
name:{
type:"string",
required:true,
minLength: 2
},
color:{
type:"string",
required:true,
unique: false
},
owners:{
model : 'user'
}
}
};
系统数据
{
pets: [
{
name: "jimmy",
color: "blue",
createdAt: "2015-07-25T05:39:57.207Z",
updatedAt: "2015-07-25T05:43:06.570Z",
owners: "55b31e06b234f3a6bcab32c6",
id: "55b3212db234f3a6bcab32c9"
}
],
name: "John",
age: "20",
createdAt: "2015-07-25T05:26:30.415Z",
updatedAt: "2015-07-25T05:26:30.415Z",
id: "55b31e06b234f3a6bcab32c6"
}
所以,船帆控制台的Select语句是
A)查找父(用户)/所有者ID的孩子(宠物)
sails> Pet.find({}).where({"owners":"55b31e06b234f3a6bcab32c6"}).exec(console.log)
null [ { owners: '55b31e06b234f3a6bcab32c6',
name: 'jimmy',
color: 'blue',
createdAt: '2015-07-25T05:39:57.207Z',
updatedAt: '2015-07-25T05:43:06.570Z',
id: '55b3212db234f3a6bcab32c9' } ]
此问题已在https://github.com/balderdashy/waterline/issues/410
中解决。B)从子ID (Pet)查找父(User)
sails> User.find({}).where({"pets":"55b3212db234f3a6bcab32c9"}).exec(console.log)
undefined
sails> User.find({}).where({"pets":"55b3212db234f3a6bcab32c9"}).exec(console.lognull []
sails> User.find({}).where({"pets.id":"55b3212db234f3a6bcab32c9"}).exec(console.log)
undefined
sails> null []
undefined
sails> User.find({}).where({"pets":{"id":"55b3212db234f3a6bcab32c9"}}).exec(console.log)
undefined
sails> null []
sails> User.find({}).where({"pets":{"id":"55b3212db234f3a6bcab32c9"}}).exec(console.log)
你能指导我在SailsJS中使用上面的select语句吗?
Thanks and Regards,
Raj
根据与SailsJS团队在https://github.com/balderdashy/waterline/issues/1102上的互动,我现在接受这个解决方案。希望事情会改善按照Github(水线)中列出的其他问题
sails> Pet.find({id: '55b3212db234f3a6bcab32c9'}).populate('owners').exec(console.log)
undefined
sails>
undefined
sails> null [ { owners:
{ name: 'MyUser',
age: '20',
createdAt: '2015-07-25T05:26:30.415Z',
updatedAt: '2015-07-25T05:26:30.415Z',
id: '55b31e06b234f3a6bcab32c6' },
name: 'jimmy',
color: 'blue',
createdAt: '2015-07-25T05:39:57.207Z',
updatedAt: '2015-07-25T05:43:06.570Z',
id: '55b3212db234f3a6bcab32c9' } ]
And for User
User.find({id:'55b31e06b234f3a6bcab32c6'}).populate('pets').exec(console.log)null [ { pets:
[ { name: 'jimmy',
color: 'blue',
createdAt: '2015-07-25T05:39:57.207Z',
updatedAt: '2015-07-25T05:43:06.570Z',
owners: '55b31e06b234f3a6bcab32c6',
id: '55b3212db234f3a6bcab32c9' } ],
name: 'MyUser',
age: '20',
createdAt: '2015-07-25T05:26:30.415Z',
updatedAt: '2015-07-25T05:26:30.415Z',
id: '55b31e06b234f3a6bcab32c6' } ]