Problems working with events, nodelib/contentlib in main.js

Enonic version: 6.13.1
OS: Windows 10

// main.js
var nodeLib = require('/lib/xp/node');
var event = require('/lib/xp/event');

event.listener({
  type: 'node.rename',
  callback: function(event) {
    var repo = nodeLib.connect({
      repoId: 'cms-repo',
      branch: 'draft' 
    });
    var eventNode = event.data.nodes.pop();
    var node = repo.get(eventNode.id);
    
    if (!node) log.info('Missing node') // <-- This kicks in every time
    else {
       /* do things with node */
    }
  }
});

The callback for the event listener is called in a different context. It might be that when calling repo.get() you don’t have permissions to read the node.

Try passing the user in the call to nodelib.connect(...).

2 Likes

Yep, passing principals to the connection fixed it :slight_smile:

This topic was automatically closed after 24 hours. New replies are no longer allowed.