Sails.js 我可以在关联中使用主键来查找吃水线吗
我创建了两个具有关联的模型: User.jsSails.js 我可以在关联中使用主键来查找吃水线吗,sails.js,waterline,Sails.js,Waterline,我创建了两个具有关联的模型: User.js attributes: { fullname: { type: 'string', required: true }, username: { type: 'string', unique: true, required: true }, email: { type: 'email', unique: true, required: true }, mo
attributes: {
fullname: {
type: 'string',
required: true
},
username: {
type: 'string',
unique: true,
required: true
},
email: {
type: 'email',
unique: true,
required: true
},
mothertongue: {
type: 'string'
},
passports: {
collection: 'Passport',
via: 'user'
},
words: {
collection: 'Words',
via: 'owners',
dominant: true
}
}
attributes: {
word: {
type: 'string',
unique: true,
required: true
},
language: {
type: 'string',
required: true
},
owners: {
collection: 'User',
via: 'words'
}
}
Words.js
attributes: {
fullname: {
type: 'string',
required: true
},
username: {
type: 'string',
unique: true,
required: true
},
email: {
type: 'email',
unique: true,
required: true
},
mothertongue: {
type: 'string'
},
passports: {
collection: 'Passport',
via: 'user'
},
words: {
collection: 'Words',
via: 'owners',
dominant: true
}
}
attributes: {
word: {
type: 'string',
unique: true,
required: true
},
language: {
type: 'string',
required: true
},
owners: {
collection: 'User',
via: 'words'
}
}
当我搜索单词所有者时,他返回空数组
Words
.find({
owners: req.session.passport.user
})
.exec(function(err, data){
if (err) {
return res.send('error');
}
res.send(data);
})
我还使用了
.populate('owners')
,但不起作用。查找用户的单词:
User.find(1).populate('words')
要查找哪些用户拥有特定的单词,请使用
Word.find(id).populate('owners', {id : ...})
您是在尝试查找与某个用户关联的单词,还是在尝试查找与某个单词关联的用户?我想显示用户的单词(例如show words的所有者id为1,但Can 1 word可以有多个用户)。这是可行的,但我想使用populate与word建立另一个关联:(好的,我可以使用add()&save()吗以一对多或任何方式代替add()&save()。如何?我在第一个之后放另一个.populate()?是的。例如:
User.find(1)。populate('passports')。populate('words')。exec(…)
很抱歉,我有很多问题:D,谢谢,但是如果我想从第二个模式获得关联,比如User.find({id:1}).populate('words').populate('words.translations')
您还不能进行嵌套填充。这项工作仍在进行中。您可以执行类似于words.find({user:1}).populate('translations')
的操作,或者在获得第一个填充后执行另一个查询。