试图提取LRS记录的某些数据,需要尝试帮助

时间:2017-03-20 20:33:23

标签: tin-can-api

我有一个90%完整且有效的脚本。我使用旧代码获取LRS语句。它是使用tincan.js的那个。我试图下拉并解析以下内容:Actor,动词,对象,时间戳和contextActivities下的id 。这是最后一个让我适合的人。你能不能看看我最后一次捕获/尝试声明并告诉我哪里出错了?谢谢。

<!DOCTYPE  html>
<!--Parsing "resumed" statements-->
<html lang='en'> 
    <head> 
        <meta charset='UTF-8'> 
        <title>Get my Statements</title> 
        <script type="text/javascript" src="build/tincan.js"></script> 
    </head> 
    <body> 
        <h1>Get statements  101</h1> 
        <div id='response'></div>
        <script>
      var lrs;

try {
    lrs = new TinCan.LRS(
        {

            endpoint: "https://lrs.adlnet.gov/xapi/",
            username: "xapi-tools",
            password: "xapi-tools",
            allowFail: false
        }
    );
}
catch (ex) {
    console.log("Failed to setup LRS object: " + ex);
    // TODO: do something with error, can't communicate with LRS
}


//Pulls all of the resumed statements

lrs.queryStatements(
    {
        params: {
            verb: new TinCan.Verb(
                {
                    id: "http://adlnet.gov/expapi/verbs/completed"
                }
            ),
            since: "2016-01-05T08:34:16Z"
        },
        callback: function (err, sr) {
            if (err !== null) {
                console.log("Failed to query statements: " + err);
                // TODO: do something with error, didn't get statements
                return;
            }

            if (sr.more !== null) {
                // TODO: additional page(s) of statements should be fetched
            }

            var container =  document.getElementById('response');
            //container.innerHTML = (err !== null ? 'ERROR' : JSON.stringify(sr.statements));
            container.innerHTML = (err !== null ? 'ERROR' : parseMyData(sr));
        }
    }
);


parseMyData = function(result) {
            var statements = result.statements;
            var output = '';
            var name,verb,activity, timestamp, context;
            for(var i=0;i<statements.length;i++){
                // check the statement for a usable name value
                // (priority = actor.name, actor.mbox, actor.account.name)
                if(statements[i].actor.name != null && statements[i].actor.name != "") {
                    name = statements[i].actor.name
                }else if(statements[i].actor.mbox != null && statements[i].actor.mbox != "") {
                    name = statements[i].actor.mbox
                }else{
                    name = statements[i].actor.account.name
                }
                // check the statement for a usable verb value
                // (priority = verb.display['en-US'], verb.id)
                try{
                    verb = statements[i].verb.display['en-US'];
                }catch(e){
                    verb = statements[i].verb.id;
                }
                // check the activity for a usable value
                // (priority = definition.name['en-US'], id)
                try{
                    activity = statements[i].target.definition.name['en-US'];
                }catch(e){
                    activity = statements[i].target.id;
                }
                try{
                    timestamp = statements[i].timestamp;
                }catch(e){
                    timestamp = statements[i].timestamp;
                }

                try{
                    context = statements[i].target.contextActivities.other['id'];
                }catch(e){
                    context = statements[i].activity.other['id'];
                }

                output +=   name + ' - ' +
                            verb + ' - ' + 
                            activity + ' - ' +
                            timestamp + ' - ' +
                            context +
                            '<br>' 
            }
            return output;
}

        </script> 
    </body> 
</html>

1 个答案:

答案 0 :(得分:0)

您正在尝试访问永不存在的属性:

statements[i].target.contextActivities

statements[i].context存在时,它的最后部分将存在于null中,除非您提前知道该声明,否则并非总是如此。在访问它们之前,应该检查null的所有属性,这样做可以删除try / catch块。属性名称本身非常稳定,并且库明确地将other值分配给已知属性。此外,contextActivities.other[0].id属性将包含一系列活动(填充时),因此您应该通过索引访问它们,因此target等。

对于actor属性,您应检查它是否是特定类的实例,因为它可以包含多种类型的对象(因此toString可以有效匹配)。通常,对于此特定任务,您可能还希望尽可能利用{{1}}方法,它们执行非常类似的工作。