我正在使用 Sails.js (0.9.8) 和 MongoDB(通过sails-mongo 适配器)来创建可以定位在树视图中的页面集合。我想将页面的路径存储在 UUID 数组中
我的模型:
module.exports = {
schema: true,
attributes: {
uuid: {
type: 'string',
unique: true,
required: true,
uuidv4: true
},
name: {
type: 'string',
required: true,
empty: false
},
path: {
type: 'array',
required: true,
array: true
}
}
}
当我保存“根”页面时效果很好(“路径”属性只有一项,因为它是根页面。这是它保存在 MongoDB 中的内容:
{
_id: ObjectId("52f853e9609fb6c0341bdfcc"),
createdAt: ISODate("2014-02-10T04:22:01.828Z"),
name: "Home Page",
path: [
"a2b23e1f-954b-49a3-91f1-4d62d209a093"
],
updatedAt: ISODate("2014-02-10T04:22:01.833Z"),
uuid: "a2b23e1f-954b-49a3-91f1-4d62d209a093"
}
但是当我想在我之前创建的页面(主页/产品)下面创建一个“子页面”时,我收到了这个错误:
MongoError:E11000 重复键错误索引:cms-project.item.$path_1 重复键:{:“a2b23e1f-954b-49a3-91f1-4d62d209a093”}
这是我发送的数据:
{ name: 'Products',
uuid: 'a004ee54-7e42-49bf-976c-9bb93c118038',
path:
[ 'a2b23e1f-954b-49a3-91f1-4d62d209a093',
'a004ee54-7e42-49bf-976c-9bb93c118038' ] }
我可能错过了一些东西,但我不知道是什么。如果我将路径存储在字符串而不是数组中,它工作得很好,但我发现它不那么优雅和方便。