我有以下模型:
Comment.js
module.exports = {
attributes: {
id : {
type : 'integer',
primaryKey : true
},
objectId : {
type : 'string',
required : true
},
comment : {
type : 'string',
required : true
}
}
};Image.js和Video.js是一样的:
module.exports = {
attributes: {
id : {
type : 'integer',
primaryKey : true
},
name : {
type: 'string'
},
comments : {
collection : 'comment',
via : 'objectId'
}
}
};当我尝试用视频或图像填充注释时,模型数组总是空的(这两个都插入了一些注释)。
Image.find({id: 1}).populate('comments').exec(function(err, image) {
console.log(image);
});或者这个..。
Video.find({id: 1}).populate('comments').exec(function(err, video) {
console.log(video);
});我想分开视频和图像模型,对于评论,我想使用组合表。
Tnx
发布于 2015-02-10 23:58:00
您可以尝试这样做:
Image.js
module.exports = {
attributes: {
id : {
type : 'integer',
primaryKey : true
},
name : {
type: 'string'
},
comments : {
collection : 'comment',
via : 'image'
}
}
};Comment.js
module.exports = {
attributes: {
id : {
type : 'integer',
primaryKey : true
},
image : {
model: 'image'
},
comment : {
type : 'string',
required : true
}
}
};查询
sails> Image.create({id:1, name: 'Image'}).then(console.log)
sails> Comment.create({id:1, comment: 'Comment', image: 1}).then(console.log)
sails>Image.find().populate('comments').then(console.log)
sails> [ { comments:
[ { id: 1,
comment: 'Comment',
image: 1,
createdAt: Wed Feb 11 2015 15:13:50 GMT-0430 (VET),
updatedAt: Wed Feb 11 2015 15:13:50 GMT-0430 (VET) } ],
id: 1,
name: 'Image',
createdAt: Wed Feb 11 2015 15:13:01 GMT-0430 (VET),
updatedAt: Wed Feb 11 2015 15:13:01 GMT-0430 (VET) } ]https://stackoverflow.com/questions/28435383
复制相似问题