当我运行此代码时,我在一个特定行(注释)上得到一个TypeError: (我只包含了这个特定的代码块,这里看到的块和计数等变量都在别处定义。)
//if semantic meaning found
if (msg.tmp.nlu.results.semantic_roles.length > 0) {
msg.tmp.nlu.results.semantic_roles.forEach( function (arrayItem){
chunk[count].semantic.push(
{
subject: arrayItem.subject.text,
object: arrayItem.object.text, // TypeError: Cannot read property 'text' of undefined"
action: arrayItem.action.text,
}
);
score++;
});
}
这是我运行forEach的msg.tmp.nlu.results.semantic_roles数组:
[{
"subject": {
"text": "I"
},
"sentence": "I worked with the team to deploy a new marketing software application.",
"object": {
"text": "to deploy a new marketing software application",
"keywords": [{
"text": "new marketing software"
}, {
"text": "application"
}]
},
"action": {
"verb": {
"text": "work",
"tense": "past"
},
"text": "worked",
"normalized": "work"
}
}, {
"subject": {
"text": "I"
},
"sentence": "I worked with the team to deploy a new marketing software application.",
"object": {
"text": "a new marketing software application",
"keywords": [{
"text": "new marketing software"
}, {
"text": "application"
}]
},
"action": {
"verb": {
"text": "deploy",
"tense": "future"
},
"text": "to deploy",
"normalized": "to deploy"
}
}]
这与使用名称'object'的arrayItem.object.text有关,但不知道如何解决它。有人可以帮忙吗?
答案 0 :(得分:0)
我无法重现您的问题,但我已尝试根据您的问题重新创建代码段。对我而言,它应该正常工作,没有TypeError: Cannot read property 'text' of undefined"
错误。希望您可以使用此工作代码来查找和解决您的问题。
// simulates chunk[count].semantic array
const semantic = [];
// simulates semantic_roles array
const semantic_roles = [{
"subject": {
"text": "I"
},
"sentence": "I worked with the team to deploy a new marketing software application.",
"object": {
"text": "to deploy a new marketing software application",
"keywords": [{
"text": "new marketing software"
}, {
"text": "application"
}]
},
"action": {
"verb": {
"text": "work",
"tense": "past"
},
"text": "worked",
"normalized": "work"
}
}, {
"subject": {
"text": "I"
},
"sentence": "I worked with the team to deploy a new marketing software application.",
"object": {
"text": "a new marketing software application",
"keywords": [{
"text": "new marketing software"
}, {
"text": "application"
}]
},
"action": {
"verb": {
"text": "deploy",
"tense": "future"
},
"text": "to deploy",
"normalized": "to deploy"
}
}];
// Slightly changed forEach loop
semantic_roles.forEach( function (arrayItem){
// Push items to the semantic array
semantic.push(
{
subject: arrayItem.subject.text,
object: arrayItem.object.text,
action: arrayItem.action.text,
}
);
// Log in console arrayItem.object.text for each item
console.log('Text:', arrayItem.object.text);
});
//Log in console semantic_roles array
console.log('semantic_roles array:', semantic_roles); // 2 items
//Log in console semantic array
console.log('semantic array:', semantic); // 2 items