Closed gp67git closed 4 years ago
@gpnick67 Thanks for providing so much detail. I edited your post to format the code examples so the are more readable.
I will have a look at this and come back to you as soon as I can (it will be a few days though).
OK, I am finding it hard to understand your example. There is some stuff to do with TypeSkill
model which doesn't seem to be relevant, and the controller code is also extraneous. So it's harder than it needs to be to read through your example and understand it. It would be easier to help you if you could reduce the example.
But here's some first suggestions:
model.getChildren()
is not a valid method.getChildren()
should be called on a model instance, not the model itself.
const skill = await Skill.findOne( { where: { id: 1 } } );
const children = await skill.getChildren();
model.rebuildHierarchy()
is asyncSo you should do await model.rebuildHierarchy()
. Without the await
, it's going to be in middle of rebuilding the hierarchy table when your .findAll()
query runs => chaos.
I don't think this is relevant to your problem, but just letting you know.
Can you please try model.findAll( { hierarchy: true } )
(without the include section etc) and see if that works?
Or, if you want to use include
, it should be as: 'descendents'
not as: 'children'
.
Children is just the direct children of that node (which is what you got). Descendents is children + children's children + children's children's children + to infinity (which is what I think you want).
model.findAll( {
include: [
{
model: model,
as: 'descendents', // NOT 'children'
hierarchy: true,
attributes: ['id', 'name', 'parentId', 'hierarchyLevel']
}
]
} ).then( /* ... */ )
I'm pretty sure this was your problem so I'm closing this issue. But feel free to comment again if I am wrong.
Hi (sorry for my english)
the problem is don't extract children nested and if call model.getChildren() the error is: (node:10432) UnhandledPromiseRejectionWarning: TypeError: model.getChildren is not a function
Can you help me ? Thanks Giuly
Use this software version:
and this node package:
My configuration options Sequilize :
My model is:
Table created:
skill
skillancestor
relation created
Controller JS is:
The result JSON is: