Expand my Community achievements bar.

Set Node Type using js-use

Avatar

Level 1

Hi everyone, im new to aem and using aem 6.5 now, and i want to ask a question, is it possible to set node type when creating new node at the same time, ive followed several tutorial, most of the are using java, but usually it also work when i convert it to js, heres the example:

this code below work on java :

 fileNode = root.addNode("image-1", "nt:file");

but thats not work in JS, it work in js if im only creating node without set the node type, like this code below :  

var fileNode = root.addNode("image-1");

ive also try to set the node type using node.setPrimaryType but it still didnt work.
any suggestion to make it work in JS ? or any article that ican read. thank you.

1 Reply

Avatar

Employee Advisor

use(function () {


var session = request.resourceResolver.adaptTo(javax.jcr.Session);
var node = request.resourceResolver.getResource("/content/dam/demo").adaptTo(javax.jcr.Node);
var node1 = node.addNode("debal-aem-resource" ,"nt:file");
var node2 = node1.addNode("jcr:content","dam:AssetContent");

session.save();
var path = node1.getPath();


return {

nodepath :path

};
});

 

Printing newly created node path to test -

 

<sly data-sly-use.custom="createnode.js">
${custom.nodepath}
</sly>

 

GeDam node path below -

DEBAL_DAS_0-1640443483658.png

 

Hope this will help. Why are you planning to create node with specific node type using js use api ?