ironbridgecorp / hl7-standard

Simple, Lightweight HL7 module for tranforming, manipulating, or creating HL7 messages.
Apache License 2.0
50 stars 13 forks source link

Create multiple OBX segment #12

Closed bryan-rigsby closed 7 months ago

bryan-rigsby commented 7 months ago

I cannot seem to create multiple OBX segements. I have tried looping through and object and also manually creating them, but it only creates the last one.

ironbridgecorp commented 7 months ago

Hi @bryan-rigsby,

If you can share with us an example of your code that is having issues we can help you out. It sounds like you are possibly overwriting the same OBX inside your loop.

Here is a simple example of creating OBX segments:

'use strict'

const fs = require('fs')
const template = fs.readFileSync(__dirname + '/template.hl7', 'utf-8')
const HL7 = require('hl7-standard');

var msg = new HL7(template)

msg.transform(err => {
  if (err) throw err

  const observations = [{
    'OBX.1': '1',
    'OBX.2': 'CE',
    'OBX.3': 'data from 1',
    'OBX.4': '',
    'OBX.5': ''
  }, {
    'OBX.1': '2',
    'OBX.2': 'CE',
    'OBX.3': 'data from 2',
    'OBX.4': '',
    'OBX.5': ''
  }, {
    'OBX.1': '3',
    'OBX.2': 'CE',
    'OBX.3': 'data from 3',
    'OBX.4': '',
    'OBX.5': ''
  }]

  for (let observation of observations) {
    let obx = msg.createSegment('OBX')
    obx.set('OBX', observation)
  }

  console.log(msg.build());
})

Output:

MSH|^~\&|SomeSystem|SomeFacility|X|HHIC|20110329082006||ORU^R01|201103290820062979|T|2.3| ... OBX|1|CE|data from 1||| OBX|2|CE|data from 2||| OBX|3|CE|data from 3|||

bryan-rigsby commented 7 months ago

thank you so much for your quick response. works like a glove! i think i was trying to set the hl7 file instead of the segment in my code

obxSegments.forEach((segment, index) => { console.log('segment in foreach', segment) let keys = Object.keys(segment); keys.forEach(key => { hl7.set(key, segment[key]) })