Closed martin-spinks closed 7 years ago
You can just pass empty object in the parent parameter this will make the node you are inserting as a root node or parent.
As far as root nodes are concerned, what i do now is just create a simple model and save it. As this is a root node, no need for any special handling. You could also pass an empty object and it will figure out that you are refering to a root node.
I know that i should probably have added a "addRoot()" method, but it is kind of useless in some ways.
I have issue creating a root node, passing {} as parent and a {"name":"testnode"} (name is the only property i have in my model right not) does not work, i obtain this error:
{ "error": { "statusCode": 500, "name": "Error", "message": "Argument passed in must be a single String of 12 bytes or a string of 24 hex characters", "stack": "Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters\n " } }
@Jcbobo88 can you provide some code?
i pushed the test project at this url
this is the curl generated from the explorer when i try to add a root node
curl -X PUT --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json' -d 'parent=%7B%7D&node=%7B%22name%22%3A%22aaaa%22%7D' 'http://localhost:3050/api/tests/addNode'
If i got this right, it might be a logic error here. You add a node to an existing parent. If you need to create a root node, you need to just use the create method and simply create a record. You then use the addNode using as a parent the node you just created.
Example, according to the documentation
var Category = app.models.Category;
//add a root node
Category.create({name : 'parent'})
.then(function(rootNode){
rootNode.addNode({name: 'parent'},
{
category : 'A sub category',
permalink : 'a-sub-category',
active : true
})
.then(function (newItem) {
console.log(newItem);
})
.catch(function (err) {
console.log('Err', err);
});
});
Same with the api. First create, then a second call to the addNode
if i make a post call (so a create) i get an other error
{ "error": { "statusCode": 500, "name": "TypeError", "message": "Cannot read property 'toString' of undefined", "stack": "TypeError: Cannot read property 'toString' of undefined\n } }
the url is
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ "name": "test" \ }' 'http://localhost:3000/api/tests'
if i pass parent as empty object instead
{ "error": { "statusCode": 500, "name": "Error", "message": "Argument passed in must be a single String of 12 bytes or a string of 24 hex characters", } }
ok, i pushed a fix, so you can have a look. I tested like so
var T = app.models.test;
T.create({name : 'parent'})
.then(function(parent){
T.addNode(parent, {name : 'child'})
.then(function(child){
console.log(child)
})
});
Then i did
T.asTree({name : 'parent'},{withParent:true})
.then(function (results) {
console.log(results);
});
and sure enough i got the tree back
pushed also on npm ?
Yeah, npm should be at 0.0.13 now
On 13 March 2017 at 17:11, Jcbobo88 notifications@github.com wrote:
pushed also on npm ?
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/mbouclas/loopback-ds-tree-mixin/issues/1#issuecomment-286137029, or mute the thread https://github.com/notifications/unsubscribe-auth/AAnOtuC8alrIYLbEIcHgOihd_P87XEdSks5rlVy5gaJpZM4IwtZB .
now it works thanks (i will open an other issue as "question" thanks
@mbouclas i think this can be closed (maybe after a readme update)
Firstly thanks so much for the mixin, having installed the mixin and played with it a little on a test env, what the is the proposed approach you recommend for creating root nodes (nodes that have no parent)?
At the moment; the addNode endpoint requires that a parent is specified; which makes sense if you're adding a node it needs to be attached to something; should there be an addRoot endpoint perhaps to allow nodes to be created with a parent of null?
How do you implement a root node at the moment if you're using the mixin, are you assuming that there is already an item in the DB (I'm using mongo also) with a parent of null?
More than happy to make a pull request so add this if you'd like?