我有以下模特:
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
答案 0 :(得分:0)
你可以试试这个:
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
}
}
};
Quering
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) } ]