我在写的ServiceNow的onChange的客户端脚本,并具有与前端客户端上的JavaScript错误的问题。我不断收到一个类型错误:无法读取的未定义的属性“u_emp_name”。变量似乎在一个点上我竟要被u_pos_office不确定的为好,但数据是正确的,并拉动上有我的代码功能没有任何影响性能变化。
这可能是造成该类型的错误?
脚本如下:
function onChange(control, oldValue, newValue, isLoading) {
var billNum = g_form.getReference('u_billet',findBilletInfo);
console.log('Emp Name: ' + billNum.u_emp_name);
console.log('OFfice: ' + billNum.u_pos_office);
console.log('Career Field: ' + billNum.u_pos_career_field);
if (isLoading || newValue == '') {
return;
}
if (oldValue != newValue){
findBilletInfo(billNum);
}
function findBilletInfo(billNum){
console.log('Bill Num' + billNum);
console.log('encumbent' + billNum.u_emp_name);
var empName = billNum.u_emp_name;
var empNameStr = empName.toString();
console.log(empName);
console.log(empNameStr);
g_form.setValue('u_organization_office',billNum.u_pos_office);
g_form.setValue('u_encumbent',billNum.u_emp_name);
g_form.setValue('u_old_career_field',billNum.u_pos_career_field);
g_form.setValue('u_old_career_specialty',billNum.u_pos_career_specialty);
g_form.setValue('u_old_occupational_series',billNum.u_pos_series);
g_form.setValue('u_old_grade',billNum.u_pos_grade);
g_form.setValue('u_old_work_category',billNum.u_pos_category);
g_form.setValue('u_old_job_title',billNum.u_pos_title);
g_form.setValue('u_losing_rater',billNum.u_emp_rater_name);
g_form.setValue('u_losing_reviewer',billNum.u_emp_reviewer_name);
}
}
这似乎这里是一个错误
var billNum = g_form.getReference('u_billet',findBilletInfo);
==> console.log('Emp Name: ' + billNum.u_emp_name);
在这种情况下,由于billNum
是异步运行getReference
不确定。见documentation for the function。
这意味着它不会立即或根本保证收益值。这可能是为什么你的记录有时,而不是别人。
您可以findBilletInfo
回调中移动这些调试日志,检查值
if (isLoading || newValue == '') {
return;
}
var billNum = g_form.getReference('u_billet',findBilletInfo);
function findBilletInfo(billNum) {
console.log('Bill Num' + billNum);
console.log('encumbent' + billNum.u_emp_name);
console.log('OFfice: ' + billNum.u_pos_office);
console.log('Career Field: ' + billNum.u_pos_career_field);
...
}
如果您在Firefox或Chrome调试,你应该能够只需登录该对象到控制台一次探索整个对象。
function findBilletInfo(billNum) {
console.log(billNum);
...
}
输出看起来像在控制台中这样的事情,你可以一次看到所有字段。