首页 > 解决方案 > 如何在 hyperledger-composer 事务处理器中定义 BusinessNetworkConnection?

问题描述

在我的 hyperledger-composer 应用程序中,我有一个事务处理器:

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

}

然而,线

const connection = new BusinessNetworkConnection();

导致以下错误消息:

ReferenceError: BusinessNetworkConnection is not defined

如何定义 BusinessNetworkConnection?

*******************************更新****************** ********************

根据 Paul O'Mahony 的评论,我在我的事务处理器函数中使用了以下代码行(为了让患者的电子邮件地址为“adam@gmail.com”):

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

查询在 queries.qry 文件中定义如下:

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

但是,查询返回“未定义”(即变量“结果”未定义)。

看在上帝的份上,代码有什么问题?我只是看不出是什么导致了这种行为。

******************************更新2************************ *******************

我必须更正自己……查询返回一些东西……但是当我想访问返回患者的 id 时,这是不可能的。那是,

result.id is "undefined"

如何访问返回患者的 ID?

标签: hyperledger-composer

解决方案


这是因为您(上面)在本机事务函数中编写客户端代码 - 您不需要设置这些。提交事务时,运行时会自动调用事务处理器函数(例如,在幕后使用 BusinessNetworkConnection API,但它已经是事务的一部分 - 您无需指定)。有关更多信息,请参阅https://hyperledger.github.io/composer/latest/reference/js_scripts - 以及常见用例和示例的示例网络 -> https://github.com/hyperledger/composer-sample-networks/树/主/包/


推荐阅读