Cannot read property '0' of undefined at Array.<anonymous>

49 views
Skip to first unread message

Francesca Ricci-Tam

unread,
Jun 15, 2021, 1:40:30 PM6/15/21
to smart-...@googlegroups.com
Hello,

my group is developing an EHR app, an for self-teaching purposes I am currently fiddling around with a JS script, written originally for v1 of the SMART on FHIR client but now trying to make it compatible with v2. (I didn't write it myself; it comes from this tutorial https://engineering.cerner.com/smart-on-fhir-tutorial/)

So far, I have successfully launched the SMART app, and the OAuth2 has gone through, creating a FHIR Client (smart.patient) object.
Then, I tried to submit a smart.patient.request() for Nancy Smart:  smart.patient.request(...input parameters...)
The resulting promise gets fulfilled, and when I use console.dir(patient) to look at what is there, I see that the Promise object does indeed contain the name, gender, address, and other requested information from the query for this patient. However, any subsequent attempts to read the properties of this patient (e.g., name) will fail, with the console error saying this property cannot be read because it is "undefined".

The script is written with JQuery; if "pt" is the result of patient.read() (which is fulfilled) and "obv" is the smart.patient.request(...) (which is also fulfilled), then trying to do something like the lines below:

$.when(pt, obv).done(function (patient, obv) {
          var fname = '';
          var lname = '';

          if (typeof patient.name[0] !== 'undefined') {
            fname = patient.name[0].given.join(' ');
            lname = patient.name[0].family.join(' ');
          }
});

this will fail with the error that patient.gender is undefined, even though I can see in the console (if I print "patient" there) that it shouldn't be. This when(...).done(...){} segment worked with the v1 FHIR client; is there anything about v2 that would break it? If so, what?

Any advice as to how I can debug this would be greatly appreciated, and let me know if anything in my problem statement was unclear (I am still new to SMART/FHIR lingo).
Thanks,

Francesca Ricci-Tam

Vladimir Ignatov

unread,
Jun 15, 2021, 2:14:40 PM6/15/21
to Francesca Ricci-Tam, SMART on FHIR
That is a nice manual but also pretty outdated. There is no point in trying to make that work v2 of the client. Perhaps you can use http://docs.smarthealthit.org/client-js/request.html as a starting point (no jQuery or fhir.js needed)


--
You received this message because you are subscribed to the Google Groups "SMART on FHIR" group.
To unsubscribe from this group and stop receiving emails from it, send an email to smart-on-fhi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/smart-on-fhir/CAMcegMRvFFaAtkTezoCRcOqdWFsRYuMHhKnZnNd0cyQpCDkAEw%40mail.gmail.com.

Josh Mandel

unread,
Jun 15, 2021, 2:16:09 PM6/15/21
to Francesca Ricci-Tam, SMART on FHIR, Dennis Patterson
+Dennis from the Cerner team in case there's interest in updating

It looks like the Cerner tutorial is out of date with respect to current versions of jquery, SMART client-js library, and FHIR. Here's a quick snippet working with the current versions of all three:


Note that

* this *just* does the bit you were asking for, doesn't include a fully updated tutorial app
* HumanName.family went from 0..* in FHIR R2 to 0..1 in FHIR R4, so it's no longer an array)
* If you want jquery, use the latest (I didn't debug the problem, but updating jquery v1 -> v3 appears to fix it)
* You probably don't need jquery these days; instead of "$.when(pt, obv).done" see the commented example for how to do this with ES6 async synatx

--

Francesca Ricci-Tam

unread,
Jun 17, 2021, 4:48:16 PM6/17/21
to Josh Mandel, SMART on FHIR, Dennis Patterson
Thank you Vladimir and Josh, your responses have been quite helpful!

I’m trying to extend my code to display other things like height, blood pressure, and so on, using a set of LOINC codes for the things I want to display — but I’m having some trouble understanding how to extract this information from the Observation resource that is returned by my request. I guess my problem is knowing where to look in this sprawling Observation resource object to know where/how I can extract the information I need based on the LOINC codes I have, since the "byCode" and "byCodes" methods are also deprecated now.

Going off the example in http://docs.smarthealthit.org/client-js/request.html for looking at a patient’s medications, I see that one could write one's own custom function for displaying medications based on SNOMED code (using the getPath function — which is now deprecated, if I understand correctly?). But it's not clear to me how one knows that one has to step specifically through "resource -> medicationCodeableConcept -> coding" of each element in the array of Medication resources to get the SNOMED information.

What resources would you recommend for me to better understand how to traverse/parse the Observation resource to search for information by codes (LOINC, SNOMED, etc)?

Cheers,

Francesca

Vladimir Ignatov

unread,
Jun 17, 2021, 5:41:45 PM6/17/21
to Francesca Ricci-Tam, Josh Mandel, SMART on FHIR, Dennis Patterson
Hi Francesca,

Here is an example of getting multiple vital signs from single request:

const client = new FHIR.Client("https://r2.smarthealthit.org");
const query = new URLSearchParams();

query.set("patient", "smart-1482713");
query.set("code", [
'http://loinc.org|29463-7', // weight
'http://loinc.org|3141-9' , // weight
'http://loinc.org|8302-2' , // Body height
'http://loinc.org|8306-3' , // Body height --lying
'http://loinc.org|8287-5' , // headC
'http://loinc.org|39156-5', // BMI 39156-5
'http://loinc.org|18185-9', // gestAge
'http://loinc.org|37362-1', // bone age
'http://loinc.org|11884-4' // gestAge
].join(","));

return client.request("Observation?" + query, {
pageLimit: 0, // get all pages
flat: true // return flat array of Observation resources
});

As for the medications, if you look at the raw JSON response you will see that it is a Bundle which has an "entry" array of entries, each of which has a resource..., hence the path we are looking for at each entry looks like "resource.medicationCodeableConcept.coding".

Finally, functions like getPath or byCode are indeed deprecated. However, they will NOT be removed, just moved elsewhere. Currently those are instance methods of the Client class but the reality is one shouldn't have to create a Client instance just to be able to use them. That is why in the next major version release they will either be made static, or will be moved to some kind of dedicated "util" namespace.



Reply all
Reply to author
Forward
0 new messages