Very Simple Mirth Functionality (Version 3.1.1.7461) - javascript

Ok, trying to get the basics down as far as how Mirth interacts with the data. Simple script below checking for a value and setting outbound to a hardcoded value when finished. This is not a real life scenario, so please don't get hung up on the why.... When running this script, I receive an error:
[2017-05-24 02:34:34,845] ERROR (transformer:?): TypeError: Cannot read property "EVN.1" from undefined.
This must be something simple, but could use some interaction if anyone cares to share. It seems to not want to identify my HL7.
Java Script
var full_evn1 = msg['EVN']['EVN.1']['EVN.1.1'].toString();
if (full_evn1 = "A01" ) {
tmp['EVN']['EVN.5']['EVN.5.1'] = "MYID"
}

I think it must be a different piece of code than the snippet you posted. You wouldn't get that error unless whatever comes before the EVN.1 is undefined. E4X has some special stuff where msg['EVN'] will be defined (as an empty XML object) even if there are no "EVN" children nodes in the parent.
Instead you probably have a separate place in your code where you're referencing something like msg['EVN'][0]['EVN.1']... The XML object supports array nature, but actually does return undefined when the index is incorrect.

Related

Why does a multi-function line of code not produce an error and simply halt execution?

I understand the below line of code is horrible coding, but my question is why does this type of code not produce a console error, and simply halt execution instead?
Example:
Assuming that there is a session storage item called mySessionItem, and its value is JSON with the property myObjectProperty in it, the following code when executed will work when the session storage item is set and has valid JSON.
let myVar = JSON.parse(sessionStorage.getItem("mySessionItem"))["myObjectProperty"] === "myValue" ? false : true;
However if the session storage is not set, or is set to invalid JSON, or valid JSON but is missing the property myProperty, it errors and halts execution.
This is normal behavior and something I would expect from such poor code. However this does not produce a console error, making it extremely difficult to track down.
Is there a reason that this does not result in a console error, is there any way I can force it to generate one programatically, and is there any easy way to track down issues like this?
I work on a large team with members of varying levels of coding ability, and as things like this enter the code-base I'd like to find better ways of tracking them since they can be quite elusive in a large application and wreak havoc everywhere.
For reference our team is using Angular 2 implemented via the Angular CLI using TypeScript. I'm not sure if one of these things plays a role in the bad code generating an error not so I wanted to make sure that I emphasized what tools we were using.
You could do something like create a class in Typescript for the session object then try to access the property since that class will always have that property.
> let myVar: boolean = new Session(JSON.parse(sessionStorage.getItem("mySessionItem"))).myObjectProperty === "myValue" ? false : true;
This uses some of the type-safety features of Typescript and you get to control what happens to the session Object in the constructor if lets say the property is not set or missing.
constructor(options: { myObjectProperty?: string } = {}){
this.myPropertyObject = options.myPropertyObject || '';
}

Converting an Array of Objects to a COM Variant Array from JavaScript

I'm currently struggeling a little with a problem concerning an ActiveX-Interface and specifically a Variant-Array. I found some answers how to achieve the conversion but unfortunately nothing worked for me so far. Maybe you can help me. Here's my problem:
I have some Objects i retrieved via the ActiveXInterface. Now one ActiveX-Method requires to have two of these objects in an array.
In VB this is what you basically do
Dim aVar1(1)
Set aVar1(0) = oReferenceLine1
Set aVar1(1) = oReferenceLine2
After that you pass this array to a function like this
Set oNewJoint = oNewMechanism.AddJoint("CATKinCylindricalJoint",aVar1)
Work with everythin i retrieve via the interface works fine, but i am unable to create an array to pass that function. One approach I found to solve this issue was this
var vbArray = new ActiveXObject('Scripting.Dictionary');
$.each(JSArray, function(index){
vbArray.add(index, JSArray[index]);
});
return vbArray.Items();
However if I do this. I just get back an undefined. vbArray gets created and I can access e.g. vbArray.Item(1) but vbArray.Items() just keeps returning undefined and calling the function
var joint = mechanism.ActiveXObject.AddJoint('CATKinCynlindricalJoint', vbArray);
just keeps returning a TypeConflict
It would be great if someone has an idea how I can put those two objects in an array and pass them as a variant array to my Application.
As often I was looking in the wrong direction for the error. Even though I couldn't (and still) can't read the contents of the vbArray it's not empty. After i corrected the typo in 'CATKinCynlindricalJoint'
I noticed that when I used the vbArray.Items() as an argument I got the error that the AddJoint-Method failed. If I just tried the Method with undefined I got a TypeConflict returned. So obviously vbArray.Items() was not really undefined.
After that i noticed that I was using a wrong argument to build my References and conversion to a vbArray did actually work. The error I made goes pretty much into detail with Catia, so I won't dive into it.

Javascript object dump returns multiple objects

I am confused with what I am seeing on a web site. From the console I type dataLayer.dump(). This outputs in the console 4 objects. Pretty sure that's weird and my research suggests a javascript method should only be able to return one object. If you need to return more than one you stick it into an array.
However, when I try dataLayer.dump()[0] I get undefined.
My question is: does anyone know if its possible to return multiple objects from one function call (not meaning an array). Or is it likely I am using the console wrong and confused myself with it? Any ideas on how to access the objects?
Why dont you type
dataLayer.dump
which will show you the code of function?
Looks like it will be something like this:
function dump(){
console.log({a:1,b:2},{a:1,b:2},{a:1,b:2},{a:1,b:2});
}

Javascript error halts $document.ready()

I'm updating an existing website running on Expression Engine. So far, I've stayed away from any code I didn't write or couldn't understand. I recently must have altered some bit of code someplace (helpful, I know) and now a block of JS I didn't write is causing an error that seems to bypass the document.ready() event. The window.load() event however is still taking place.
In the Chrome DevTools Console, the error "Uncought TypeError: Cannot call method 'replace' of UNDEFINED" points to the definition of a function "fixedEncodeURIComponent" pasted below.
$("#MessageContainer.Counted").counter({
type: 'char',
goal: 250,
count: 'down'
}).change(function(){
var TEMP = fixedEncodeURIComponent($(this).val());
$("#Message").val(TEMP);
});
var TEMP = fixedEncodeURIComponent($("#MessageContainer.Test").val());
$("#Message").val(TEMP);
function fixedEncodeURIComponent (str) {
str=str.replace(/"/g, '');
return encodeURIComponent(str).replace(/[!'()*]/g, escape);
}
As I interpret the error, this function is being passed a variable that is not a string. I added an alert(str) to the function definition and the result was UNDEFINED as I expected. The first of several unknowns for me is which call to the function 'fixedEncodeURIComponent' is being passed a bad variable. I assume that it's the first call, but that's just a guess. It so happens that this first call contains a syntax I have never encountered before. I don't know how to interpret what happens when $(this) is passed as a function argument.
Any insights would be greatly appreciated. Also, if there's more information you need please let me know. The client's site is password protected but I can include any code you request.
Thank you.
I'm taking a guess that the }); on line 3 is exiting a document.ready context. If that's the case then your second call to fixedEncodeURIComponent may be getting called before the DOM is even loaded.
Start by wrapping
var TEMP = fixedEncodeURIComponent($("#MessageContainer.Test").val());
$("#Message").val(TEMP);
in a
$(function() {
// code
});
block. If that doesn't work, check that #MessageContainer.Test actually matches an element. Since this is code you inherited, the class name "Test" clues me in that the block in question might be a remnant of someone trying to debug an issue and maybe it should have been removed.
I suspect $("#MessageContainer.Test") since it looks like its supposed to be an ID selector instead of what it actually is when jQUery parses it(which is an ID selector combined with a class selector). $("MessageContainer\\.Test") allows you to select an element with ID MessageContainer.Test

Uncaught TypeError: Cannot call method 'hasChildNodes' of undefined

I am trying to read a XML using ajax request in jquery. Below is the code,it's working fine in IE but when I run this on Chrome i am facing this error
Uncaught TypeError: Cannot call method 'hasChildNodes' of undefined
$.ajax({
type: "GET",
url: "tree1.xml",
dataType:"xml",
success: function(xml){
root=xml.documentElement;
childs=root.childNodes;
for(var i=0;i<childs.length;i++){
z=childs[i].childNodes;
adChilds(z,childs,oNode);
function adChilds(a,b,c){
if(a[i].hasChildNodes()){
adNode(b[i].nodeName,c);
var oNode_ch=oNode1;
for(var j=0;j<a.length;j++){
child1=a[j].childNodes;
adNode(child1[0].nodeValue,oNode_ch);
}
}
else{adNode(a[0].nodeValue,oNode);}
}
}
error is pointing at this line.
if(a[i].hasChildNodes()){
Can anyone suggest me where am I going wrong.
Thanks in advance!
You're doing this:
z=childs[i].childNodes;
then, in the first line of adChilds(z,childs,oNode);, you're doing this:
if(a[i].hasChildNodes()){
But, i isn't an index into the children of a. It's an index into the parent's of a's children. Thus, if the parent of a doesn't have the same number of children as a has children, you will go out of index.
I don't know exactly what you're trying to accomplish in adChilds() so I'm not sure what fix to suggest, but I presume you that if you want to deal with the children of a, you should get the number of children of a and make sure you only access the number that actually exist
Some coding suggestions:
I'd strongly suggest you use real variable names. Names like a, b, c and z for intermediate variables are cryptic and make your code hard to read.
All local variables should be preceded with var at first definition (or defined at the top of the function), otherwise they become global variables which is asking for trouble, especially with asynchronous callback functions.
When you see errors that aren't obvious to you upon first inspection of your code, then set a breakpoint in your favorite debugger and step through the code and examine the state of the variables to see exactly why you're getting the error. If you don't know how to use a debugger, learn. They are built into most browsers, easy and absolutely essential to efficient debugging.

Categories