Javascript (Connector) way to access just the key fields?

4 posts / 0 new
Last post
Last seen: 2 months 1 week ago
Joined: 05/17/2021
Posts: 2
Javascript (Connector) way to access just the key fields?

I am using RTI Connector for javascript and trying to find a way to access just the key fields when reading in a message. I know this can be done in C++ and Java, but in the Javascript API Documentation, this is only mentioned for when the sample has been disposed

Otherwise, the getJson() function gets ALL fields, not just the instance key fields. Is there an equivalent function for Javascript that performs the same operation as the key_value() function in the C++ API?

Last seen: 3 weeks 15 hours ago
Joined: 10/22/2018
Posts: 90

What is the function you are referring to in C++ and Java?

I don't think that the example in the documentation that you supplied for JavaScript (here) does what you think it does:

// The output and input are using the following type:
// struct ShapeType {
//     @key string<128> color;
//     long x;
//     long y;
//     long shapesize;
// }

output.instance.set('x', 4)
output.instance.set('color', 'Green')
// Assume that some data associated with this instance has already been sent
output.write({ action: 'dispose' })
await input.wait()
let sample = input.samples.get(0)

if ('instance_state') === 'NOT_ALIVE_DISPOSED') {
    //'valid_data') will be false in this situation
    // Only the key-fields should be accessed
    let color = sample.get('color') // 'Green'
    // The fields 'x','y' and 'shapesize' cannot be retrieved because they're
    // not part of the key
    // You can also call getJson() to get all of the key fields in a JSON object.
    // Again, only the key fields returned within the JSON object should
    // be used.
    let keyValues = sample.getJson() // { color: 'Green', x: 0, y: 0, shapesize: 0 }

The call to getJson() there will still contain a JSON object with all of the key/value pairs (that is, JSON key/value pairs) contained in sample, but when the instance state is disposed, it comes with the disclaimer that only the key values (that is, IDL key values) should be accessed.


Last seen: 2 months 1 week ago
Joined: 05/17/2021
Posts: 2

Thanks for the reply. The function I'm referring to is key_value().

Ok, I see what you mean about the getJson() function. You're correct that I misunderstood what the documentation was saying about that.

Howard's picture
Last seen: 9 hours 39 min ago
Joined: 11/29/2012
Posts: 448

The key_value() method that you refer to takes an "instance_handle".  This method is only useful when you don't have the actual value of data, but a handle to the instance...for which you want to know the key value.

(in many Listener callbacks, you may get a handle to the instance that's relevant to the callback)

In any case, key_value() will return a full data sample object with just the key fields filled out.  NOTE: your code will still need to know which members of the data structure comprise the key of the data type.  It's not that you only get the key values back.  You get an entire data structure in which only the key members have any real value.

So, what you're asking for is not possible in C/C++ either...