将函数添加到加载的node.js模块

时间:2016-08-08 10:43:53

标签: node.js

大约2年前,我写了一个node.js模块,它加载了一个现有的模块(jsts),并为它添加了一些功能。这是一个最小的例子:

// Step 1 ==================================
var Ibc1 = require('ibm-blockchain-js');
var ibc = new Ibc1(/*logger*/);             //you can pass a logger such as winston here - optional
var chaincode = {};


// ==================================
// configure ibc-js sdk
// ==================================
var options =   {
    network:{
        peers:   [{
            "api_host": "127.0.0.1",
            "api_port": 49155,
            //"api_port_tls": 49157,
            "id": "vp4"
        }],
        users: null,
        options: {quiet: true, tls:false, maxRetry: 1}
    },
    chaincode:{
        zip_url: 'https://github.com/IBM-Blockchain/learn-chaincode/archive/master.zip',
        unzip_dir: 'learn-chaincode-master/finished',    
        git_url: 'https://github.com/IBM-Blockchain/learn-chaincode/finished'

    }
};

// Step 2 ==================================
ibc.load(options, cb_ready);

// Step 3 ==================================
function cb_ready(err, cc){                             //response has chaincode functions

chaincode = cc;

   console.log(JSON.stringify(cc));

   chaincode.deploy('init', ['Hi hyperledger'], null, cb_deployed);


// Step 5 ==================================
function cb_deployed(){

    console.log(JSON.stringify(chaincode));
    console.log('sdk has deployed code and waited');

    console.log('******** Writing to chaincode Now **********');
    chaincode.invoke.write(["mykey","Hi Ledger Systems"],function(err,data){
    console.log('write response:', data);

   readData();


});
}



function readData()
{

console.log('\n\n**** Waiting 7 seconds before reading **** \n\n');
setTimeout(function () {
  console.log('\n\n**** Start reading **** \n\n');

  chaincode.invoke.read(["mykey"],function(err,data){
   console.log('read response:', data);

 });
}, 7000)
}


  }

我在节点(v0.10.18)中运行它并打印

global.jsts = require("jsts");
global.jsts.algorithm.test = function() {console.log("hi")}
global.jsts.algorithm.test();

现在,我在nodejs(v4.2.6)中运行相同的代码并打印:

hi

有没有办法让它适用于当前版本的nodejs?

编辑:我也做了:

TypeError: global.jsts.algorithm.test is not a function

这是输出:

global.jsts.algorithm.x = 1
console.log(global.jsts.algorithm)

0 个答案:

没有答案