Javascript Sails JS-相同模型和属性的深度填充
让我解释一下。我们的想法是有类别,每个类别都可以有父类别和子类别,但我们不知道这棵树能有多深。例如:Javascript Sails JS-相同模型和属性的深度填充,javascript,node.js,model,sails.js,populate,Javascript,Node.js,Model,Sails.js,Populate,让我解释一下。我们的想法是有类别,每个类别都可以有父类别和子类别,但我们不知道这棵树能有多深。例如: ->Clothes -->Men --->Kids ---->Newborns ----->Etc, etc -->Women -->Unisex 因此我认为我的Category.js模型可以具有以下属性: module.exports = { attributes: { name: { type: 'string',
->Clothes
-->Men
--->Kids
---->Newborns
----->Etc, etc
-->Women
-->Unisex
因此我认为我的Category.js模型可以具有以下属性:
module.exports = {
attributes: {
name: {
type: 'string',
required: true,
unique: true
},
products: {
collection: 'product',
via: 'category'
},
parentCategory: {
model: 'category'
},
subCategories: {
collection: 'category',
via: 'parentCategory'
}
}
};
当我得到所有分类时:
Category.find({}).populate('subCategories').exec(........
我得到了一个所有类别及其子类别的列表,但我也想得到子类别的子类别,就像我以前写的树层次结构一样,但是我得到的是
服装{子类别:[男性:{},女性:{},男女通用:{}]}
男人{
但是在衣服里面,我有男人,这是正确的。但是在这个男人的物体里面,我没有“孩子”
我说的清楚吗?填充方法只在一个级别(目前)起作用,因此您无法填充和对象,然后在内部填充另一个。我有一个类似的功能,我发现并编写了此代码,也许它可以帮助您:
return Object.findOne({
id: id
}).populateAll().then(function (result) {
var otherObject= otherObject.find(result.id).then(function (otherResult) {
return otherResult;
});
return [result, otherResult];
}).spread(function (house, otherResult) {
result= result.toObject();
result.otherResult= otherResult;
return result;
});