无法在 Hyperledger Composer 中执行 HTTP 请求

Cannot Perform HTTP Request in Hyperledger Composer

我正在尝试创建一个业务网络,它通过从 URL (https://random-number-api.mybluemix.net/random) 请求一个随机数来演示不确定的行为。

但是,当我从中调用事务时,出现以下错误:

TypeError: request.get is not a function

文档(https://hyperledger.github.io/composer/latest/integrating/call-out)中提到request是全局的,可以直接使用

测试 URL,

curl -X GET https://random-number-api.mybluemix.net/random

我正在使用 Composer 0.18.2。

代码如下。整个业务网络定义可在此处找到:https://github.com/caveman7/nondeterministic

async function createStock(request) {
    const factory = getFactory();
    const namespace = 'org.nondeterministic';

    const stock = factory.newResource(namespace, 'Stock', request.stockId);
    const priceAsStr = await request.get({uri: 'https://random-number-api.mybluemix.net/random'});
    stock.price = parseInt(priceAsStr);
    //stock.price = getPrice();
    console.log('@debug - Stock Price Generated is: USD' + stock.price);
    const ar = await getAssetRegistry(namespace + '.Stock')
    await ar.add(stock);
}

const getPrice = () => {
  const price = Math.floor((Math.random() * 10000) + 1);
  return price;
}

可能是因为您将 request 作为对象提供给 TP。如上图或https://github.com/caveman7/nondeterministic/blob/master/lib/script.js#L22

错误,因为它 'not' 得到了一个名为 get 的方法?。它应该是 async function createStock(createStock ) 以匹配事务装饰器中的参数名称(从上面 link 跟踪 github 脚本文件) - 然后你应该能够访问 request.get()如您所料(因为 request.get 出现在 0.18.1 IIRC 中)。