我最近搬到了MarkLogic 9.0.2,现在我将所有触发逻辑从xqy移动到sjs。
这是我的问题,我在sjs中有一个触发器,它是在xqy中创建的,如下所示:
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()
)
触发器代码尝试查找刚刚创建的文档以检查某些内容。
请注意,uri似乎“就在那里”......
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))
}
};
;
现在触发器被激活并被触发但在日志中数据对象为空。
日志:
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]
如果我在查询控制台中执行相同的代码,则可以正常工作......
我缺少什么?