简体   繁体   中英

Hyperledger Composer Query Access Returned Resource ID

I had a question regarding the return value of a query in hyperledger composer.

For reference my query:

query findCountOfficer {
  description: "find count officer asset given name"
  statement:
    SELECT org.example.CountOfficer
        WHERE (name == _$nameParam)
}

My querying:

let countOfficerRecord = await query('findCountOfficer', {nameParam: countOfficerName})

I am trying to retrieve the ID of the count officer from the count officer record parameter.

Does anyone know how to access the identifier or fields of a resource returned from a query?

Or if not that how to have a query return the employeeId? (ie select employeeID)

What I've tried:

When I print countOfficerRecord I get Resource {id=org.carm.CountOfficer#1}

I want to access the 1 ID. However if I call countOfficerRecord.id or countOfficerRecord.employeedId (as employeeId is the identifier in the model file) I get undefined .

As this is a Resource I also tried functions from the documentation such as getIdentifier() and isResource() and got an error that these weren't functions for countOfficerRecord .

Thanks!

If you want to get the identifier then try this

console.log( countOfficerRecord.$identifier)

if you want the whole record of countOfficerRecord

then try this

console.log([countOfficerRecord])

It looks like you are invoking the toString() method on the resource object that you have (either explicitly or implicitly via whatever mechanism you use to print the object). The output you see is the result of that toString invocation and not a JSON respresentation of the object, you can interact with the resource object using it's api's, eg

console.log(countOfficerRecord.getIdentifier()); console.log(countOfficerRecord.name)

The above example references $identifier which isn't a recommended approach as this is an internal field name and could change in the future. You should use the api's of a resource object to obtain information.

The api reference for a resource can be found here

https://hyperledger.github.io/composer/latest/api/common-resource

I tried this and it worked. Queries usually return an array. Therefore, you first reference the array and then get the identifier. countOfficerRecord[0].$identifier

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM