Cannot read property 'MyProperty' of undefined - javascript

In my Angular2 template, I have the following binding:
{{Stringify(result)}}
{{result.MyProperty}}
Stringify is a function that returns JSON.stringify of the input object. The Stringify function returned a JSON string that shows the name and value of MyProperty.
However, the second line returns a
TypeError. Cannot read property 'MyProperty' of undefined in {{result.MyProperty}}.
JSON.stringify clearly shows that this property / field exists, so why am I getting an error?

If it's there it can be accessed and JS wouldn't throw
Try instead
{{result?.MyProperty}}
maybe Angular makes an attempt to access result.MyProperty before result has a value while Stringify(result) doesn't choke on null.
When result is updated in the meantime (maybe because the value was received from the server, the view would update before you can recognized that an empty string was shown before.
Your question doesn't provide enough context to know.
See also
https://github.com/angular/angular/issues/791
https://angular.io/docs/ts/latest/guide/template-syntax.html#!#expression-operators (from a comment below - thanks to #MarkRajcok )

Related

Can't access object properties even though I can see they exist

I can't access any of the object properties, keep getting undefined.
I have tried
console.log(JSON.parse(this.$store.state.user.userId));
and
console.log(JSON.parse(this.$store.state.user[0].userId));
When I do
console.log(JSON.parse(this.$store.state.user.userId));
I get
"SyntaxError: Unexpected token u in JSON at position 0"
When I just do
console.log(JSON.parse(this.$store.state.user));
I get the object and I can see the properties. It's just that whenever I try to access them I get undefined.
When I just do console.log(JSON.parse(this.$store.state.user)); I get the object and I can see the properties.
This means that this.$store.state.user contains JSON string which describes the user object.
Thus JSON.parse(this.$store.state.user.userId) is incorrect. In this case you are trying to get property userId from string, getting undefined and JSON.parse function fails on the first symbol, which is 'u'.
You should use JSON.parse(this.$store.state.user).userId instead.

What object has a String representation of `{name=value}` in JavaScript?

I receive a complex value in JavaScript (inside the JDK Nashorn engine), that I have to interact with. That value prints to the console as {shown=true}. When I say typeof value I receive object as an answer. When I say Object.keys(value); I receive a TypeError: {shown=true} is not an Object in .... Whey I say value.shown or value["shown"] I always receive a null.
What is type is this mysterious object, and how do I access the value of the "shown" property correctly?
Unfortunately, it is not easy to create a simple example and I cannot debug interactively... Any help is highly appreciated!
Edit:
JDK is JavaSE-1.8.
Calling JSON.parse(value); results in
javax.script.ScriptException: SyntaxError: Invalid JSON: <json>:1:1 Expected , or } but found s
{shown=true}
^
Assuming what you got is a Java object, you should be able to call value.getClass() to get its Java class.
From its string representation, it might be an instance of java.util.HashMap or similar. If so, you should be able to access the value of the "shown" property via value.get("shown").

Javascript TypeError: Cannot read property 'indexOf' of undefined Exchange Message

I have the following script on a page that I want to replace some system language.
<script>
function xchangemessage2()
{
xval = jQuery('[ID*="_UserMessageText"]').html();
xvalpos = xval.indexOf("We could not find your information in our system. Please search again or contact us for assistance.");
xvallen = xval.length;
if (xvalpos > 0)
{
jQuery('[ID*="_UserMessageText"]').html(xval.substring(0, xvalpos+xvallen) + ' If you provided a valid user name, an email will be sent to the email address on file.')
}
}
Sys.Application.add_load(xchangemessage2);
</script>
I keep getting
Javascript TypeError: Cannot read property 'indexOf' of undefined.
TypeError: The TypeError object represents an error when a value is not of the expected type.
You are trying to access property indexOf of variable xval which is undefined.
String.prototype.indexOf(): The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.
Array.prototype.indexOf(): The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
undefined: The global undefined property represents the primitive value undefined. It is one of JavaScript's primitive types.
Since undefined object is not a string or an array thus using indexOf() method gives an error. Value of xval is undefined as HTML element with id _UserMessageText is not found in DOM.
Would recommend to do go through JavaScript documentation once before using it's features to avoid errors and using JavaScript at best.
Also checkout jQuery features.
The TypeError and the script tags mean that your JS is running before your HTML code is loaded. To fix this, simply put the script tags at the very bottom of your body element, so the rest of the HTML loads before your JS.

Returns undefined on a object prop even though it exists-JS

I'm having the same issue as mentioned here: Can't access object property, even though it exists. Returns undefined
However in my case it's a property with data.hostId.id, where data comes in as full object and hostId is shown undefined when its actually present in the json object, here's how it looks:
As you can see, I'm trying to access the qgUSerHost(at the very end) from the object and it throws undefined. I tried setTimeout, not working…
I also tried the solution mentioned in that post but nothing helped.
Any ideas on this one?
I think you should access that property like this.hostAsset["qgUserHost.id"] because of the . in the property name, so it wouldn't try to access the property id from some qgUserHost object.
'this.hostAsset.qgUserHost.id' != 'this.hostAsset.qgUserHost'

Cannot read property 'concat' of undefined using knockout

I created a computed observable rather than having a extra code in the view and now when I run the page I get an error in the console stating:
"Uncaught TypeError: Cannot read property 'concat' of undefined"
Here is what I have so far:
self.email = ko.observable(initialData.userEmail || '');
self.emailMailto = ko.computed(function(){ return 'mailto:'+self.email();});
I know that email observable is returning the appropriate data but I am not sure if it is referring to emailMailto as undefined.
this is my view:
<p><a data-bind="attr:{href:emailMailto},text:email"></a></p>
The code you've posted is not directly related to your problem.
What's happening here is that some code wants to use the .concat() method on a variable.
.concat() is a prototype of Array, which means that the code in question expects an array.
undefined means that, instead of an array, nothing has been passed to the code. This can mean two things:
Either an expected function parameter has not been passed, e.g. the method is declared as function doSomething(withValue) and you are calling it as function doSomething() (without a value).
Or the code tries to process a property of an object which is not set.
As the code causing the error has been working before and you apparently didn't modify it, I would guess that it's the latter case: You're tossing around an object which is missing a property that should be there and should be an array.

Categories