我第一次使用SailsJS。我已经建立了一些带有关联的模型,并且一切正常。几乎。
这是我的项目模型:
module.exports = {
attributes: {
title: { type: 'string', required: true },
subtitle: { type: 'string', required: false },
image: { model: 'image' },
bgColor: { type: 'string', required: false },
tabPos: { type: 'integer', required: true, defaultsTo: 0 },
featured: { type: 'boolean', required: true, defaultsTo: false },
contentBlocks: { collection: 'contentBlock', via: 'project' },
position: { type: 'integer', required: true }
}
};
这是我的ContentBlock模型:
module.exports = {
attributes: {
project: { model: 'project' },
copy: { type: 'string', required: true },
icon: { type: 'integer', required: false },
images: { collection: 'image' },
bgColor: { type: 'string', required: false },
position: { type: 'integer', required: true }
}
};
最后,我的Image模型:
module.exports = {
attributes: {
filename: { type: 'string', required: true },
alt: { type: 'string', required: true },
device: { type: 'string', required: true, defaultsTo: 'laptop' },
deviceColor: { type: 'string', required: false }
}
};
除了项目端点不在有效负载中包含contentBlock图像之外,所有关联都工作得很好。它甚至不包含一系列ID。它完全忽略了属性:
[
{
contentBlocks: [
{
project: "5406ffb56d109ad149a14362",
copy: "Lorum ipsum",
// expecting array of image ids here
// but its completely missing
position: 0,
createdAt: "2014-09-03T11:49:08.162Z",
updatedAt: "2014-09-03T12:13:55.279Z",
id: "540700346d109ad149a14363"
}
],
image: {
filename: "test1_320x480.jpg",
alt: "A test image",
device: "laptop",
createdAt: "2014-09-03T11:36:13.608Z",
updatedAt: "2014-09-03T11:36:13.608Z",
id: "5406fd2d6d109ad149a1435e"
},
title: "Project Alpha",
subtitle: "A test project",
position: 0,
tabPos: 0,
featured: false,
createdAt: "2014-09-03T11:47:01.155Z",
updatedAt: "2014-09-03T12:13:35.972Z",
id: "5406ffb56d109ad149a14362"
}
]
然而,contentBlock端点确实包含图像。
[
{
images: [
{
filename: "test2_320x480.jpg",
alt: "Another test image",
device: "ipad",
deviceColor: "#FFF",
createdAt: "2014-09-03T11:36:50.875Z",
updatedAt: "2014-09-03T11:39:38.216Z",
id: "5406fd526d109ad149a1435f"
},
{
filename: "test3_320x480.jpg",
alt: "The last test image",
device: "iphone",
deviceColor: "#000",
createdAt: "2014-09-03T11:43:09.599Z",
updatedAt: "2014-09-03T11:43:09.599Z",
id: "5406fecd6d109ad149a14361"
}
],
project: {
title: "Project Alpha",
subtitle: "A test project",
image: "5406fd2d6d109ad149a1435e", // sails is populating this association fine
position: 0,
tabPos: 0,
featured: false,
createdAt: "2014-09-03T11:47:01.155Z",
updatedAt: "2014-09-03T12:13:35.972Z",
id: "5406ffb56d109ad149a14362"
},
copy: "Lorum ipsum",
position: 0,
createdAt: "2014-09-03T11:49:08.162Z",
updatedAt: "2014-09-03T12:13:55.279Z",
id: "540700346d109ad149a14363"
}
]
我唯一覆盖的是现阶段的模型,其他一切都是默认的。
非常感谢任何帮助。