I moved to MarkLogic 9.0.2 recently, now I am moving all my trigger logic from xqy to sjs.
Here my issue, I have a trigger in sjs, it is created in xqy like this:
xquery version "1.0-ml";
import module namespace trgr="http://marklogic.com/xdmp/triggers"
at "/MarkLogic/triggers.xqy";
xdmp:log("TRGR: Installing tp-firstTimeSeen"),
trgr:create-trigger("tp-01-firstTimeSeen", "Trigger to Alert First time a device is seen by a tp-sensor ",
trgr:trigger-data-event(
trgr:collection-scope("tresspasser"),
trgr:document-content("create"),
trgr:post-commit()
),
trgr:trigger-module(xdmp:database(), "/", "/triggers/tp-01-first-time-only.sjs"),
fn:true(), (: enabled / disabled :)
xdmp:default-permissions(),
fn:false()
)
The trigger code tries to find the document that was just created to check some stuff.
Please note that the uri seems "just there"...
The trigger code in sjs:
declareUpdate();
// load utilities
//var scc = require("/lib/sccsslib.sjs");
var uri;
xdmp.log(fn.concat('TRESSPASSER-TRIGGER ON ',uri, ' was triggered...'))
var data = fn.doc(uri);
xdmp.log(fn.concat('data : ',data))
var event = data.toObject();
var sensorId = event.event.sensorid;
var sData = event.event.sensordata;
for (i = 0; i < sData.length; ++i){
// search for each mac adres in the database
var mac = sData[i].mac;
// search all events from this sensor
var hit = cts.search(cts.andQuery([cts.collectionQuery("tresspasser"),
cts.jsonPropertyValueQuery("sensorid",sensorId),
cts.jsonPropertyValueQuery("mac",mac)])).toArray();
if (fn.empty(hit)) {
// we have never seen this mac before
// send alert
xdmp.log('TODO: SEND ALERT')
} else {
xdmp.log(fn.concat('SEEN BEFORE : ',mac))
}
};
;
Now the trigger is activated and gets triggered but in the log the data object is empty.
Log:
2017-12-17 15:43:59.707 Info: TRESSPASSER-TRIGGER ON /data/sensors/tresspasser/12345678/b39b-749c-acc2.json was triggered...
2017-12-17 15:43:59.707 Info: data :
2017-12-17 15:43:59.711 Notice: JS-JAVASCRIPT: var sensorId = event.event.sensorid; -- Error running JavaScript request: TypeError: Cannot read property 'sensorid' of undefined
2017-12-17 15:43:59.711 Notice:+in /triggers/tp-01-first-time-only.sjs, at 15:27 [javascript]
2017-12-17 15:43:59.711 Notice:+in /triggers/tp-01-first-time-only.sjs [javascript]
If I execute the same code in the query console it works...
What am i missing?