试图提取 LRS 记录的某些数据片段,需要 catch-try 帮助
Trying to pull certain data pieces of an LRS record, need catch-try assistance
我有一个已完成 90% 并且可以正常工作的脚本。我正在使用旧代码获取 LRS 语句。就是使用tincan.js的那个。我正在尝试下拉并解析以下内容:Actor、动词、对象、时间戳和 id under contextActivities。这是最后一个让我感到不适的人。你能看看我最后的 catch/try 陈述并告诉我哪里错了吗?谢谢。
<!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>
您正在尝试访问永远不会存在的 属性:
statements[i].target.contextActivities
当它存在时,它的最后一部分将存在于 statements[i].context
中,除非您提前知道声明,否则并不总是如此。在访问它们之前,几乎所有的属性都应该检查 null
,这样做将允许您删除 try/catch 块。 属性 名称本身非常稳定,库明确地将 null
值分配给已知属性。此外,other
属性 将包含一组活动(当它被填充时),因此您应该通过索引访问它们,因此 contextActivities.other[0].id
等
对于 target
属性 您应该检查它是否是特定 class 的实例,因为它可以包含多种类型的对象(actor
但他们有效匹配)。一般来说,对于此特定任务,您可能还希望尽可能利用 toString
方法,它们的工作非常相似。
我有一个已完成 90% 并且可以正常工作的脚本。我正在使用旧代码获取 LRS 语句。就是使用tincan.js的那个。我正在尝试下拉并解析以下内容:Actor、动词、对象、时间戳和 id under contextActivities。这是最后一个让我感到不适的人。你能看看我最后的 catch/try 陈述并告诉我哪里错了吗?谢谢。
<!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>
您正在尝试访问永远不会存在的 属性:
statements[i].target.contextActivities
当它存在时,它的最后一部分将存在于 statements[i].context
中,除非您提前知道声明,否则并不总是如此。在访问它们之前,几乎所有的属性都应该检查 null
,这样做将允许您删除 try/catch 块。 属性 名称本身非常稳定,库明确地将 null
值分配给已知属性。此外,other
属性 将包含一组活动(当它被填充时),因此您应该通过索引访问它们,因此 contextActivities.other[0].id
等
对于 target
属性 您应该检查它是否是特定 class 的实例,因为它可以包含多种类型的对象(actor
但他们有效匹配)。一般来说,对于此特定任务,您可能还希望尽可能利用 toString
方法,它们的工作非常相似。