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").
Related
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.
When I get element type, I can't ask if element is Class:
var theElement as EA.Element;
theElement = Repository.GetTreeSelectedObject();
If (theElement.Type=="Class"){}
I get error "Object was expected". Also tried to create string variable, but didnt work... Any idea? Thanks.
TreeSelectedObject() does not necessarily return an EA.Element.
It could be EA.Diagram, EA.Package, EA.Attribute,...
You should first check whether the returned object is not null.
Then you can use theElement.ObjectType to distinguish between the different object types
See also http://www.sparxsystems.com/enterprise_architect_user_guide/13.5/automation/objecttypeenum.html
I am trying to return the value under the key 'str' in an Object but I am having trouble accessing the value.
This is what is returned in the console:
Currently I am using a map function to go over the array and just return the _str value like so:
let idx = currentArray.map(function(x) {
return x._id._str;
});
However it is still returning the value as an object. How can I get just the value of the _str key?
Here is the full array without specifying the id field. This is what is returned if you jsut return 'x' in the map function.
You've clarified that the screenshot is of x._id. So to access _str, you'd use x._id[0]._str: The _str property is in the object referenced by the 0 property (the first entry in the array x._id refers to).
Note that in general, _-prefixed properties are meant not to be accessed by code outside the code responsible for the objects in question. You don't seem to be responsible for them, so accessing those properties is likely to make your code rely on undocumented properties that may change in the next "dot" release of whatever lib you're using. It's just convention, but it's a very common convention.
If you right click on the property, most browser consoles offer the ability to copy property path.
Based on this SO post and the docs, it appears that you can probably use x._id.str.
If I understand correctly, you are receiving the str value but it is an object instead of the string literal. In other words, you are getting _str: "598..." instead of "598....". A possible solution would be to use the mongo javascript function to convert the str value to a string.
In your case, I think something like return x._id.str; may work as _id is a MongoID.ObjectID.
I've also linked the documentation below for reference.
https://docs.mongodb.com/manual/reference/method/ObjectId/
Here's a relevant SO answer as well: Convert ObjectID (Mongodb) to String in JavaScript
I think you should write x[_id]._str because _id is one of the array objects.
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 )
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.