View CCDA using ccdaservice Error: property undefined

Hello,
I am on OpenEMR 5.0.1 patch 7 on Windows with the ccdaservice setup per the readme file. When I click on “View CCDA” from a patient in the Care Coordination module, the page returns these errors:

C:\Apache24\htdocs\neuroemr310\ccdaservice\serveccda.js:1650
npiProvider = all.primary_care_provider.provider[0].npi;
^

TypeError: Cannot read property ‘primary_care_provider’ of undefined
at genCcda (C:\Apache24\htdocs\neuroemr310\ccdaservice\serveccda.js:1650:23)
at C:\Apache24\htdocs\neuroemr310\ccdaservice\serveccda.js:1891:19
at C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xmljson\lib\to_json.js:33:10
at Parser. (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\lib\xml2js.js:255:20)
at Parser.emit (events.js:160:13)
at SAXParser.saxParser.onclosetag (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\lib\xml2js.js:225:24)
at emit (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\node_modules\sax\lib\sax.js:615:33)
at emitNode (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\node_modules\sax\lib\sax.js:620:3)
at closeTag (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\node_modules\sax\lib\sax.js:861:5)
at SAXParser.write (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\node_modules\sax\lib\sax.js:1293:29)
C:\Apache24\htdocs\neuroemr310\ccdaservice\serveccda.js:1651
oidFacility = all.encounter_provider.facility_oid;
^

TypeError: Cannot read property ‘encounter_provider’ of undefined
at genCcda (C:\Apache24\htdocs\neuroemr310\ccdaservice\serveccda.js:1651:23)
at C:\Apache24\htdocs\neuroemr310\ccdaservice\serveccda.js:1891:19
at C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xmljson\lib\to_json.js:33:10
at Parser. (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\lib\xml2js.js:255:20)
at Parser.emit (events.js:160:13)
at SAXParser.saxParser.onclosetag (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\lib\xml2js.js:225:24)
at emit (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\node_modules\sax\lib\sax.js:615:33)
at emitNode (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\node_modules\sax\lib\sax.js:620:3)
at closeTag (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\node_modules\sax\lib\sax.js:861:5)
at SAXParser.write (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\node_modules\sax\lib\sax.js:1293:29)
C:\Apache24\htdocs\neuroemr310\ccdaservice\serveccda.js:1655
let demographic = populateDemographic(pd.patient, pd.guardian, pd);
^

TypeError: Cannot read property ‘patient’ of undefined
at genCcda (C:\Apache24\htdocs\neuroemr310\ccdaservice\serveccda.js:1655:46)
at C:\Apache24\htdocs\neuroemr310\ccdaservice\serveccda.js:1891:19
at C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xmljson\lib\to_json.js:33:10
at Parser. (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\lib\xml2js.js:255:20)
at Parser.emit (events.js:160:13)
at SAXParser.saxParser.onclosetag (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\lib\xml2js.js:225:24)
at emit (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\node_modules\sax\lib\sax.js:615:33)
at emitNode (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\node_modules\sax\lib\sax.js:620:3)
at closeTag (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\node_modules\sax\lib\sax.js:861:5)
at SAXParser.write (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\node_modules\sax\lib\sax.js:1293:29)
^

Any ideas on how to fix them?
Thanks!

Couple things.

  • The provider that creates encounters must have their Provider flag checked in Users. This is a bug in OpenEMR that if it is not set then a provider lookup based on encounter will fail.
  • Many times reporting to medicaid (to states) require a Facility OID. They cost but the facility id in facility setup needs to be populated so try using the facility NPI (or something) there.
  • I don’t remember if the provider in Demographic->Choices has to be populated or if this is just an error because the encounter provider doesn’t have the provider checked in Users.

So of these requirements could be loosened if I ever get time to go back to ccda and address. Sorry

@sjpadgett
Thanks for your help. I have all of the provider fields populated. However, still getting the error. I opened up the node.js console and this is what it displayed:

C:\Apache24\htdocs\neuroemr310\ccdaservice>node serveccda.js
127.0.0.1:54281
toJson error: Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char: sLen: 8467
toJson error: Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char: eLen: 8496
toJson error: Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char: eLen: 8695
toJson error: Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char: eLen: 7694
toJson error: Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char: >Len: 7830
toJson error: Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char: vLen: 7845
toJson error: Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char: >Len: 7890
toJson error: Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char: zLen: 7900
toJson error: Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char: dLen: 7890
toJson error: Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char: hLen: 7910
toJson error: Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char: rLen: 7910
toJson error: Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char: >Len: 7905
toJson error: Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char: tLen: 7895
toJson error: Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char: _Len: 7899
C:\Apache24\htdocs\neuroemr310\ccdaservice\serveccda.js:1650
npiProvider = all.primary_care_provider.provider[0].npi;
^

TypeError: Cannot read property ‘primary_care_provider’ of undefined
at genCcda (C:\Apache24\htdocs\neuroemr310\ccdaservice\serveccda.js:1650:23)

at C:\Apache24\htdocs\neuroemr310\ccdaservice\serveccda.js:1891:19
at C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xmljson\lib\to_js

on.js:33:10
at Parser. (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modul
es\xml2js\lib\xml2js.js:255:20)
at Parser.emit (events.js:160:13)
at SAXParser.saxParser.onclosetag (C:\Apache24\htdocs\neuroemr310\ccdaservic
e\node_modules\xml2js\lib\xml2js.js:225:24)
at emit (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js\node
_modules\sax\lib\sax.js:615:33)
at emitNode (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js
node_modules\sax\lib\sax.js:620:3)
at closeTag (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules\xml2js
node_modules\sax\lib\sax.js:861:5)
at SAXParser.write (C:\Apache24\htdocs\neuroemr310\ccdaservice\node_modules
xml2js\node_modules\sax\lib\sax.js:1293:29)

Does that help narrow it down any?

yep, got to Modules->Manage Modules select Carecoordination Config then Settings tab and set your Primary Care Provider in the pull down. Set others while there and ensure localhost is set in Mirth IP(i’ll have to default that in code one day).
Did I miss that in set up doc?

Jerry,
After some research it looks like the culprit is caused from Byte-Order-Mark (BOM), a 3-byte “Zero width no-break space” Unicode character which Windows systems automatically prepend to UTF-8 files.

I was able to remove the character by making this change to the
serveccda.js file on line 1873 from

xml = xml.replace(/(\u000b|\u001c)/gm, “”).trim();

To

xml = xml.replace(/(\u000b|\u001c|\ufeff)/gm, “”).trim();

@sjpadgett
I spoke too soon, sorry! That code doesn’t seem to fix it. It just happened to work after I tried it again with a patient that only had 1 encounter. It looks like problem is that the connection is sending the data in multiple packets, so the ccdaservice needed to concatenate the packets into a variable first before attempting to process the XML as a whole.

I got it working by modifying the eventData function that is called when a packet of data is received. It copies the packet to a new var xml_complete and then checks the end of the packet for the </CCDA> tag to know if this is the last packet before processing:

var xml_complete=’’;

function eventData(xml) {
    xml = xml.replace(/(\u000b|\u001c)/gm, "").trim();
    // Sanity check from service manager
    if (xml === 'status' || xml.length < 80) {
        conn.write("statusok" + String.fromCharCode(28) + "\r\r");
        conn.end('');
        return;
    }
  xml_complete += xml.toString();
  if (xml.toString().match(/\<\/CCDA\>$/g)) {
  	// ---------------------start--------------------------------
  	let doc = "";
  	xml_complete = xml_complete.replace(/\t\s+/g, ' ').trim();

  	to_json(xml_complete, function (error, data) {
  		// console.log(JSON.stringify(data, null, 4));
  		if (error) { // need try catch
  			console.log('toJson error: ' + error + 'Len: ' + xml_complete.length);
  			return;
  		}
  		doc = genCcda(data.CCDA);
  	});

  	doc = headReplace(doc);
  	doc = doc.toString().replace(/(\u000b|\u001c|\r)/gm, "").trim();
  	//console.log(doc);
  	let chunk = "";
  	let numChunks = Math.ceil(doc.length / 1024);
  	for (let i = 0, o = 0; i < numChunks; ++i, o += 1024) {
  		chunk = doc.substr(o, 1024);
  		conn.write(chunk);
  	}
  	conn.write(String.fromCharCode(28) + "\r\r" + '');
  	conn.end();
  }
}

Nice catch. I believe the buffer on node side of socket is 16K so you were running out of buffer. I’ll see if I can’t get this in next patch. Thanks