how to define BusinessNetworkConnection in hyperledger-composer transaction processor?

冷暖自知 提交于 2019-12-11 07:29:05

问题


In my hyperledger-composer app, I have a transaction processor:

async function doSomething(transaction) {

    //some code


    // the following line results in error message:
    const connection = new BusinessNetworkConnection();
    await connection.connect('admin@tmy-network');
    const result = await connection.query(selectPatientByEmail, { inputValue: email });

    //some more code

}

However, the line

const connection = new BusinessNetworkConnection();

causes the following error messsage:

ReferenceError: BusinessNetworkConnection is not defined

How can I define the BusinessNetworkConnection?

*******************************UPDATE**************************************

Following the comment by Paul O'Mahony, I used the following line of code in my transaction processor function (in order to get the patient with the email address 'adam@gmail.com'):

let result = await query('selectPatientByEmail', {
    "email": "adam@gmail.com"    
});

The query is defined in the queries.qry file as follows:

query selectPatientByEmail {
    description: "Select the patient with the given email address"
    statement:
        SELECT org.comp.app.Patient
            WHERE (email == _$email)
}

However, the query returns "undefined" (i.e. variable "result" is undefined) .

What for god's sake is wrong with the code? I just can't see what might be the causing this behaviour.

***************************Update2*****************************************

I have to correct myself ... the query returns something ... but when I want to access the id of the returned patient, this is not possible. That is,

result.id is "undefined"

How can I access the id of the patient returned?


回答1:


this is because you are (above) writing client code inside a native transaction function - you don't need to set these. Transaction processor functions are automatically invoked by the runtime when transactions are submitted (eg using the BusinessNetworkConnection API under the covers, but it is already part of the transaction - you don't need to specify) . See https://hyperledger.github.io/composer/latest/reference/js_scripts for more info - and the sample networks for common use cases and examples -> https://github.com/hyperledger/composer-sample-networks/tree/master/packages/



来源:https://stackoverflow.com/questions/51479770/how-to-define-businessnetworkconnection-in-hyperledger-composer-transaction-proc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!