首页 > 解决方案 > 如何收听作曲家的事件

问题描述

我正在使用 Composer v0.19.12。我正在尝试构建一个应用程序来与区块链交互以监听事件。我的脚本如下:

const BusinessNetworkConnection = require("composer-client")
  .BusinessNetworkConnection;
this.businessNetworkConnection = new BusinessNetworkConnection();
//this.CONNECTION_PROFILE_NAME = 'admin@tutorial-network';
this.cardName ='admin@tutorial-network';
this.businessNetworkIdentifier = 'tutorial-network';
this.businessNetworkConnection
  .connect(
    this.cardName,
    this.businessNetworkIdentifier,
    "admin",
    "adminpwd"
  )
  .then(result => {
    this.businessNetworkDefinition = result;
    console.log("BusinessNetworkConnection: ", result);
  })
  .then(() => {
    // Subscribe to events.
    this.businessNetworkConnection.on("event", events => {
      console.log("**********business event received**********", events);
    });
  })
  // and catch any exceptions that are triggered
  .catch(function(error) {
    throw error;
  });

当我尝试运行代码时,出现如下错误:

(node:2414) UnhandledPromiseRejectionWarning: Error: Failed to load connector module "composer-connector-undefined" for connection profile "admin@tutorial-network". Error: Cannot find module 'composer-connector-undefined'
    at connectionProfileStore.load.then (/home/user/devTutorial/composer-eventlistner/node_modules/composer-common/lib/connectionprofilemanager.js:150:38)
    at <anonymous>
(node:2414) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2414) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

你有什么解决办法吗?谢谢。

根据 Paul 的建议,我发布了他建议的命令的输出:

docker ps
CONTAINER ID        IMAGE                                                                                                                COMMAND                                     NAMES
78d0e5cee683        dev-peer0.org1.example.com-tutorial-network-0.0.3-ccfa7421a9e100115b8beac460d72ce0ce0e48cd38b00ab9b1ea6b7de230bc0a   "/bin/sh -                                  dev-peer0.org1.example.com-tutorial-network-0.0.3
a270270d9d53        hyperledger/fabric-peer:x86_64-1.1.0                                                                                 "peer node051/tcp, 0.0.0.0:7053->7053/tcp   peer0.org1.example.com
1445aeac6480        hyperledger/fabric-couchdb:x86_64-0.4.6                                                                              "tini -- /tcp, 0.0.0.0:5984->5984/tcp       couchdb
7acfb5f37ccf        hyperledger/fabric-orderer:x86_64-1.1.0                                                                              "orderer" 050/tcp                           orderer.example.com
e0d699b7591b        hyperledger/fabric-ca:x86_64-1.1.0                                                                                   "sh -c 'fa054/tcp                           ca.org1.example.com

node --version
v8.11.3

 npm ls -g --depth=0
/home/user/.nvm/versions/node/v8.11.3/lib
├── composer-cli@0.19.12
├── composer-playground@0.19.12
├── composer-rest-server@0.19.12
├── generator-hyperledger-composer@0.19.12
├── jsdoc@3.5.5
├── npm@6.2.0
├── wscat@2.2.1
└── yo@2.0.4

任何建议,将不胜感激。

标签: hyperledger-composer

解决方案


模式(例如 in index.js)将是:

const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection;
this.bizNetworkConnection = new BusinessNetworkConnection();
this.cardName ='admin@YOUR-NETWORK';
this.businessNetworkIdentifier = 'YOUR-NETWORK';

this.bizNetworkConnection.connect(this.cardName)
.then((result) => { 
//You can do ANYTHING HERE eg.

console.log("BusinessNetworkConnection: ", result);
})
.catch((error) => {
throw error;
});

// then the event stuff
this.bizNetworkConnection.on('event',(evt)=>{
 console.log('The notification sent from sendEvent was: '+evt.theMessage);
});

(运行监听器node index.js

我的模型文件中的事件定义是:

event BasicEvent {
o String theMessage
}

和我的交易职能sendEvent

/**
*
* @param {com.biz.sendEvent} basicEventTransaction
* @transaction
*/

function basicEventTransaction(basicEventTransaction) {
    var factory = getFactory();

    var basicEvent = factory.newEvent('com.biz', 'BasicEvent');
    basicEvent.theMessage = "Guess who?";
    emit(basicEvent);
}

推荐阅读