web stats
Code Templates: Functions - Error Undefinded - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 11-17-2015, 03:16 PM
clarkmirth clarkmirth is offline
Mirth Newb
 
Join Date: Jun 2014
Posts: 10
clarkmirth is on a distinguished road
Default Code Templates: Functions - Error Undefinded

Mirth Connect Server 3.30.7801

I created a function that will be used as a callback for the .forEach function. I want it to take the elements I created on an object and map them to the channelMap.

Example:
Code:
var demographics = {};

try {
	demographics.pat_first = msg['PID']['PID.5']['PID.5.2'].toString(); 
	demographics.pat_last = msg['PID']['PID.5']['PID.5.1'].toString();
	demographics.dob = msg['PID']['PID.7']['PID.7.1'].toString();
} catch (e) {
	logger.error(document, e);
	demographics = {};
}
Object.keys(demographics).forEach(channelMapCallback(key, index, array));
The Callback function is defined in the code template below
Code:
/**
	this function is a callback for a forEach loop. 
	It will create channel maps for all the keys on an object

	Example:
	Object.keys(someObject).forEach(channelMapCallback);

	@param {String} key - key each key for element in forEach loop
	@param {String} index - index for each element in forEach loop
	@param {String} array - the array that is beeing traversed in forEach loop
	@return nothing
*/
function channelMapCallback(key, index, array) {
	channelMap.put(key, validate( array[key] , '', new Array()));
	return;
}
When I drop and drag the function and use it. I get an error:

Code:
ReferenceError: "channelMapCallback" is not defined.
Things I have tried:

originally was calling the call back like this:

Code:
Object.keys(demographics).forEach(channelMapCallback);
I received the same error.

I also tried removing the return in the function thinking that was an issue.

I could possibly be overlooking something simple. I've been known to have a few typos.

Last edited by clarkmirth; 11-17-2015 at 04:10 PM.
Reply With Quote
  #2  
Old 11-18-2015, 01:53 AM
bobekos bobekos is offline
OBX.1 Kenobi
 
Join Date: Oct 2014
Posts: 26
bobekos is on a distinguished road
Default

Make sure your have set the dependencies in your channel.
Reply With Quote
  #3  
Old 11-18-2015, 09:23 AM
clarkmirth clarkmirth is offline
Mirth Newb
 
Join Date: Jun 2014
Posts: 10
clarkmirth is on a distinguished road
Default forEach allows for this to be passed & Library permissions

Ok figured it out. Main issue was with the library permissions on the code template. I forgot to check the channels I wanted to use the function on.

Also looked at documentation of forEach. It allows you to pass a object that will be used for this.

final function:

Code:
/**
	Modify the description here. Modify the function name and parameters as needed. One function per
	template is recommended; create a new code template for each new function.

	@param {String} arg1 - arg1 description
	@return {String} return description
*/
function channelMapCallback(key, index, array) {
	channelMap.put(key, validate( this[key] , '', new Array()));
}
Calling it like this:

Code:
var demographics = {};

demographics.pat_first = msg['PID']['PID.5']['PID.5.2'].toString(); 
demographics.pat_last = msg['PID']['PID.5']['PID.5.1'].toString();
demographics.dob = msg['PID']['PID.7']['PID.7.1'].toString();


Object.keys(demographics).forEach(channelMapCallback, demographics);
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 12:47 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Mirth Corporation