因此,我作为一名实习生正在从事一个项目,该项目使用 Bugsnag 来捕获其 Web 应用程序中的错误。我试图修复一个无法重现的错误。它只影响我们使用该产品的所有客户中的 2 个人。这段代码不是我写的,我只是想修复其中的错误。错误是 TypeError·Uncaught TypeError: Cannot set property 'selected' of undefined
,它在第二行的这个函数中。
function selectInstaller(installer) {
installers[installer.id].selected = true;
selectedInstaller[installer.id] = installer;
}
假设有一个个人列表,用户可以选择一个人来在日历上显示该特定个人的事件。一旦选择了用户,就会调用此函数。
function handleSelectInstaller(event) {
var $btn = $(this);
var $li = $btn.closest('li');
$li.toggleClass('active');
var installerAttributes = $btn.closest('li').data();
if($li.hasClass('active')) {
selectInstaller(installerAttributes);
fetchInstallerCalendar();
} else {
previousInstallerCount = Object.keys(selectedInstaller).length;
unselectInstaller(installerAttributes);
syncView();
}
}
如您所见,handleSelectInstaller 函数随后调用 selectInstaller 函数,然后发生错误。就像我说的那样,这种错误很少见,而且我无法重现错误。我认为该特定人员的数据库可能有问题,但我的经理查看了它并说它看起来是正确的。他还尝试登录到用户的帐户并尝试以这种方式重现它,但没有给他带来任何问题(因此他无法在他的计算机上重现它)。但是当用户登录到他们的计算机时,Bugsnag 仍然会报告它。关于如何重现此错误以便解决问题的任何想法?或者它可能完全超出我的控制范围?
编辑:这是加载页面时调用的内容。最后一个函数将自动选择登录的用户。然后当用户(安装者)选择或取消选择他们的名字时, handleSelectInstaller 函数被调用。堆栈跟踪显示我们进入 handleSelectInstaller 函数。
function init(options) {
var options = options || {};
baseUrl = serverpath + "v1.0/calendar_events";
selector = options.selector || '#calendar';
colors = {
default: '#59595C',
disabled: '#ff9f89'
};
dateFormat = 'YYYY-MM-DD';
type = $(selector).data('type');
installers = {};
installerArray = [];
selectedInstaller = {};
cache = {};
cacheCalendar = {};
currentMonth = {start: null, end: null}
standardInstallerObject = {jobs: {}, daysOfWeekDisabled: {}, time_off: {}, color:null}
previousInstallerCount = 0;
setup();
}
function setup() {
setupListeners();
setupDatePickers();
$('.installer-name').text('Select Installer');
syncEventTypes();
syncJobStatuses();
fetchInstallers(setupCalendar);
}
function fetchInstallers(callback) {
$.ajax({
url: serverpath + 'v1.0/users?role=installers',
type: "GET",
dataType: 'json'
}).done(function(response) {
loadInstallers(response);
if(typeof callback === 'function') callback();
})
.fail(function(xhr) {
handleError(xhr);
$('#calendar-loading-indicator').fadeOut();
})
.always(function() { });
}
function loadInstallers(response) {
for(var i in response) {
var installer = response[i];
var id = installer.id;
//need to extend / copy object since the object would be stored by reference not by value
var copiedObject = $.extend(true, {}, standardInstallerObject);
var copiedObjectCalendar = $.extend(true, {}, standardInstallerObject);
cache[id] = copiedObject;
cacheCalendar[id] = copiedObjectCalendar;
if(installers[id]) continue;
installers[id] = installer;
installerArray.push(installers[id]);
}
if(type == 'installer') {
var installer = installers[$.User.id()];
selectInstaller(installer);
$('.installer-pick-list li[data-id="'+ $.User.id() +'"]').addClass('active');
}
}
最佳答案
您使用的是 ajax 调用吗?也许使用 setTimeout 模拟一个长的 ajax 调用,看看你是否得到同样的错误。因此,用 setTimeout 调用包装那里的 ajax 调用,并将其设置为 4000 或类似的值。这是我正在谈论的一个很好的例子。 using SetTimeout with Ajax calls
setTimeout(function(){checkData()}, 5000);
function checkData(){
$.ajax({
//
});
}
因为加载安装程序功能已经运行,并且用户通过首先选择安装程序来击败他们,您可以执行类似的操作。
面向公众
$.ajax({
url: //something
type: "GET",
dataType: 'json'
}).done(function(response) {
loadInstallers(response);
$.publish('Installer.set_installer_info');
if(typeof callback === 'function') callback();
})
});
订阅
$.subscribe('Installer.set_installer_info', function() {
$('body').on('click', '.installer-calendar-container .installer-pick-list .selectable', handleSelectInstaller);
});
关于javascript - 重现前端错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43767463/