Chrome - Cannot read property 'status' of undefined even with check first - javascript

I see a lot similar questions for this particular javascript error, but my issue is I've already implemented the "fixes" and it still occurs, but only randomly.
The error:
Uncaught TypeError: Cannot read property 'status' of undefined
Before referencing the status property like so json.status, in all instances I precede it with:
if ( typeof json.status != 'undefined' )
So in theory, there should be no reason for it to ever throw that error if I'm checking first in all situations.
When I click the line number it's referencing in the console, it goes to a blank screen, so it's been hard to troubleshoot. If I expand open the error stack thing, it references my jquery dataTables a bunch, but I don't know if that's because something is failing prior to it or not.
How can I troubleshoot this deeper, or catch that error properly so it doesn't halt all my AJAX calls that come after it? (requires a refresh when it happens...)

Cuz you're not providing the full code, I cannot understand so clearly your situation.
Anyway, the error simply showing that json is undefined (due to something wrong in the coding logic, ...).
To quick fix the error, you can change your code to do null-check for json as well:
if (json && (typeof json.status != 'undefined')) {...}
Or a better way:
if (json && json.status) {...}

Related

How to solve Type error in an ionic framework

How can I solve this error:
core.js:1449 ERROR Error: Uncaught (in promise): TypeError: Cannot
read property 'slug' of undefined TypeError: Cannot read property
'slug' of undefined
My code:
this.WooCommerce.getAsync("products?filter[category]=" + this.category.slug).then((data) =>{
console.log(JSON.parse(data.body));
well to me this confirms what I first said. you are indeed pointing to a child of an object within an observable (whether you know what observables are or not) (observables sometimes move all the way up the priority list and happen before hydration of any other var).
so here's the precautions I like to take to make sure things don't go badly: splitting things up.
Also, here's a "hack" I like to use on a general basis in order to ensure I can continue my dev cycle unhindered and have a better notion of where things are failing other than just "undefined":
this.category['slug'];
I never go this far but this is also allowed :
this['category']['slug'];
see this is different because it will actually return the undefined instead of failing there at a JS level.
now to me it seems pretty obvious that you need to ascertain this.category.slug in the instant it's being evaluated isn't in a state in it's lifeline prior to it's initialization :
var myFunctionToLogSlug = () => { //here you could pass this.category.slug as an arg but
//that's the same as simply calling in within the body of this method because your
//context (this) is the same, ergo the pointer and timeframe are also the same.
console.log("is this.category.slug empty ? : ", this.category.slug);
return this.category.slug;
};
this.WooCommerce.getAsync(
"products?filter[category]=" + myFunctionToLogSlug()).then(data =>{
console.log(JSON.parse(data.body));
});
once you've ascertained this.category.slug is indeed undefined at the point it is called, you can either move this.WooCommerce.getAsync to a time-place you know this.category.slug to be defined, or move this.category.slug's hydration to the inside of the getAsync.
hope this helps ! :)

Uncaught TypeError: Cannot read property 'find' of null.jquery

Getting the error "Uncaught TypeError: Cannot read property 'find' of null." in the console. Underlining this line of code containing the error:
if ($sResults.find("product").length > 0)
Anyone have any idea why this is happening. Im a beginner so any adivce would help. Thanks.
if ($sResults.find("product").length > 0) {
// We have some search results
// The products are in $sResults
} else {
// We don't have any results
// Hide the table
$("#mainBody").find("table").hide();
$("#mainBody").append("<h3>There are no search results. </h3>");
}
$sResults is null, for whatever reason. Perhaps you are hitting an API or otherwise dynamically getting that info and nothing is being returned, or perhaps you have that info available locally and it is not being passed correctly - either way, you are calling .find on null and that is the issue. It may be okay, if fetching the info dynamically, to have no results, right? If it's not - that is one of two problems because your first (and current
) problem is that your code is breaking. It's breaking because you are not protecting it by checking to see if $sResults HAS a value before you run .find()
For a more precise answer on WHY you are getting a null value, we would need more code ;)
Try this:
if ($sResults && $sResults.find("product").length > 0) {
// We have some search results
// The products are in $sResults
} else {
// We don't have any results
// Hide the table
$("#mainBody").find("table").hide();
$("#mainBody").append("<h3>There are no search results. </h3>");
}

Extjs Intermittent error

Using Extjs 4.1, an intermittent error is occurring.
Sometimes it occurs, but most of the time it does not.
It always occurs when I start the application, so I press F5 and everything returns to normal.
Using this reference to debug the error, I noticed that it occurs at line 29590.
The error message is very generic:
Uncaught TypeError: Cannot read property 'dom' of null
at constructor.finishRender (ext-all-debug-w-comments.js:29590)
at constructor.finishRenderItems (ext-all-debug-w-comments.js:39796)
at constructor.finishRender (ext-all-debug-w-comments.js:40889)
at constructor.finishRenderChildren (ext-all-debug-w-comments.js:44526)
at constructor.afterRender (ext-all-debug-w-comments.js:29331)
at constructor.callParent (ext-all-debug-w-comments.js:6194)
at constructor.afterRender (ext-all-debug-w-comments.js:36521)
at constructor.finishRender (ext-all-debug-w-comments.js:29625)
at constructor.finishRenderItems (ext-all-debug-w-comments.js:39796)
at constructor.finishRender (ext-all-debug-w-comments.js:40889)
Here is an error print
What must be causing this?
I would have made this a comment, but it doesn't fit.
There is a wide variety of reasons I could think of. Most of them are based on asynchronous calls. You definitely have to provide more information for the question to be answerable.
Obviously, me.el is null, which it shouldn't be. So first you have to find which component is causing the problem. For this, you should exchange line 29590 against something like this:
try {
me.container = Ext.get(me.el.dom.parentNode);
} catch(e) {
console.log(me.id);
console.log(me.itemId);
console.log(me.xtype);
...
console.log(me);
throw e;
}
This should give you an idea which component(s) would be affected.
Then show us the code of that component. Also check whether you modify that component's config from outside the component, e.g. from a store load operation or other asynchronous tasks. Plus you should look at whether overrides for the component and its ancestors are loaded from separate JS files - maybe they are sometimes loaded before, sometimes after the finishRender has been called, and fix exactly this error.
These are only a few of the possible reasons.

Cannot read property 'getWidth' of undefined

I have below code which is defined inside a function which actually set width of parent component and do layout...this function is called from 4 places and at one place there is scenario that below error comes, you can see i have checked for undefined but still it is going inside this IF and i get error :( please help identify issue
Ext Js 3.4.2
ERROR
Uncaught TypeError: Cannot read property 'getWidth' of undefined
CODE
if((typeof Ext.getCmp("cmp1") !== 'undefined') && (typeof Ext.getCmp("cmp2") !== 'undefined')){
Ext.getCmp("cmp1").setWidth(Ext.getCmp("cmp2").getWidth()-2);
Ext.getCmp("cmp1").doLayout();
}else{
//Call self again in 3 sec once render
//setTimeout("doLayoutofcustomPanel()", 3000);
}
I able to figure out issue actually component was created but element (EL) was not present initially so it able to cross my IF check I need to put check as below to ensure element is present for which I need to fetch width. This worked fine
typeof Ext.getCmp("cmp2").el !== 'undefined'
I am using ExtJs 3.4.2
Not sure which version of ExtJs you are using, but its better to use itemId
itemId is scoped locally to the container -- avoiding potential
conflicts with Ext.ComponentManager which requires a unique id
and then query to get it.

Unable to reproduce TypeError: 'undefined' is not an object

Google Analytics shows that ~12% of our total users are affected by a Javascript bug of:
TypeError: 'undefined' is not an object
90% of the browsers are Safari 7534.48.3, 10% are Mozilla compatible agent. 75% of the errors come from iPhones, 23% from iPads. 1% from Macintosh, the other 2% is from iPod etc. None of the devices run Linux or Windows.
I have tried enabling debug mode in safari on both an iPhone and iPad but not able to reproduce the bug.
Here is a link to a page Google Analytics claims is showing the error. If anyone can consistently reproduce the error here I will be super happy because just a line number would be enough to get me started debugging.
Can anyone think of any other ways I can try to debug this? Thanks all
For the curious among us I'm using this code to send errors to GA -- Warning: Possible self promotion.
Update: TypeError: 'undefined' is not an object (evaluating 'safari.self.tab.canLoad')
Managed to get that out of it once when clicking around, mostly on an iphone whilst clicking "Change country.."
Update: Solved this by making sure the element was available in the dom. Turns out the ajax call on success was trying to write to an element that wasn't available.
I have kept a solid record of Unable to reproduce TypeError: 'undefined' is not an object here
Undefined
The undefined type has only one value: undefined. It’s the default value that all variables have after a declaration if no value is assigned to them. You can also assign the value undefined to any variable, but in general, this should be avoided, because if we need to mark a variable as not holding any meaningful value, we should use null.
Let declaredVar;
console.log(typeof declaredVar); // -> undefined
declaredVar = 5;
console.log(typeof declaredVar); // -> number
declaredVar = undefined;
console.log(typeof declaredVar); // -> undefined
The undefined value can also be returned by the typeof operator when a non-existent variable is used as an argument.
Console.log(typeof notDeclaredVar); // -> undefined
console.log(notDeclaredVar); // -> Uncaught ReferenceError: notDeclared is not defined
In your functions.js, you have this:
storage_get = function(key) {
var store = (window.SAFARI ? safari.extension.settings : localStorage);
var json = store.getItem(key);
if (json == null)
return undefined;
try {
return JSON.parse(json);
} catch (e) {
log("Couldn't parse json for " + key);
return undefined;
}
}
undefined is NOT a JavaScript keyword. It's a variable that (most of the time) happens to be undefined. You can't use undefined like this. Consider what would happen if you replaced it with pinkelephant, as that is the exact thing that's happening here.

Categories