hyperledger-composer - 如何在 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?
解决方案
这是因为您(上面)在本机事务函数中编写客户端代码 - 您不需要设置这些。提交事务时,运行时会自动调用事务处理器函数(例如,在幕后使用 BusinessNetworkConnection API,但它已经是事务的一部分 - 您无需指定)。有关更多信息,请参阅https://hyperledger.github.io/composer/latest/reference/js_scripts - 以及常见用例和示例的示例网络 -> https://github.com/hyperledger/composer-sample-networks/树/主/包/
推荐阅读
- python - 类似于 R 的 bpa(基本模式分析)的 Python 包 - 包
- javascript - 如何获取表字段的 SUM?- Angularjs 和 Sqlite
- python-3.x - len和size的区别
- java - JPA 的问题
- laravel - 虚拟主机在 nginx ubuntu 18.04 中不起作用
- regex - 如何使用 Ruby gsub 和正则表达式进行部分字符串替换
- python - 如何从 mpts udp(一组带有 id 的程序)到带有一个程序 id(spts)的视频流
- javascript - 如何在javascript中将多个对象转换为键值对的数组[]
- javascript - 一旦评估为真,“删除”测试条件
- react-native - 在抽屉堆栈之间传递参数 react-navigation