TypeError: Cannot read property 'onClose' of undefined
wasuint opened this issue · 8 comments
Hi, Thanks for building this nice component. The component is working as documented. Only in the console.log I get this error: "s-alert.js:50 Uncaught TypeError: Cannot read property 'onClose' of undefined".
It does not break my application and also the alert is shown. Just wandered why this error showed up and maybe I am doing something wrong?
When i startup a form and I do an insert, the alert says the data is saved successfully and I do not get an error in the console.log. When I enter new data in the same form again, the alert says the data is saved successfully, but the second time, the error shows up. And when I repeat the process, I get the same error.
The error comes after the alert disappeared.
I use it in Meteor, version: juliancwirko_s-alert\3.1.3
I could not find anything on google, so that is why I posted it here. If you require more information, please let me know.
Thanks in advance. 👍
There should be a data check before getting onClose, but I wonder why there isnt the alert object in the moment of closing the alert. Dou you close the alerts manually by closeAll or they just autoclose?
I don't close it manually, So i must be autoclose.
The line of code where the error is:
onClose = sAlert.collection.findOne(alertId).onClose;
The whole part is:
if (document.hidden || document.webkitHidden || !$('#' + alertId).hasClass('s-alert-is-effect')) {
onClose = sAlert.collection.findOne(alertId).onClose;
sAlert.collection.remove(alertId);
invokeOnCloseCb();
} else {
$('.s-alert-box#' + alertId).removeClass('s-alert-show');
closingTimeout = Meteor.setTimeout(function () {
$('.s-alert-box#' + alertId).addClass('s-alert-hide');
}, 100);
$('.s-alert-box#' + alertId).off(EVENTS);
$('.s-alert-box#' + alertId).on(EVENTS, function () {
$(this).hide();
onClose = sAlert.collection.findOne(alertId).onClose;
sAlert.collection.remove(alertId);
Meteor.clearTimeout(closingTimeout);
invokeOnCloseCb();
});
}
My call to the alert is like this:
return sAlert.success(Helpers.t('DataSaved'));
t = translation function to TAPi18N i created:
t: function (keyToTranslate){
//this way you do not have to write TAPi18n.__ everytime. Just Helpers.t
return TAPi18n.__(keyToTranslate)
}
With your code it looks everything is ok. I'll add data check here: onClose = sAlert.collection.findOne(alertId).onClose;
so it won't fire this error.
But I still wonder why there isn't an alert in the collection at this moment. It should be there. I'll try to test it more.
If you can write the correction here, I can update the file and try if it is sufficient.
The line: onClose = sAlert.collection.findOne(alertId).onClose; is already in the code, and it is the place where the error occurs.
yes, this is what I mean. I need to check if the data exists in Salert.collection and then get the onClose. It probably fix the error for you. I just wonder why there isn't data in sAlert.collection at that point. I'll try to push changes asap.
Thanks, no hurry here....
Ok, you can check version 3.1.4 (be sure that you have it instaled, sometimes there are problems with updates in Meteor)
Hi Julian, I just updated your package and tried it again. It (still) works perfect and no error messages anymore in the log. So great work. Thanks for your time and effort. 👍 And have a nice day.