从事务处理器函数调用REST API

问题描述 投票:0回答:1

我正试图通过事务处理器函数从开放天空api获取数据。我一直在看文档:https://hyperledger.github.io/composer/integrating/call-out

由于我正在对此API执行GET请求,因此我无需使用请求发送任何数据。这就是我将参数数据设置为空对象的原因:

var data = {};

但是当我运行这个时,我收到以下错误:

错误:Serializer.toJSON只接受Resource的实例。

此错误是因为toJSON(resource, options)函数需要资源。这可以在以下链接中看到:

https://hyperledger.github.io/composer/jsdoc/module-composer-common.Serializer.html#toJSON__anchor

我还查看了post()函数的代码,我将在下面复制:在224行:https://hyperledger.github.io/composer/jsdoc/composer-runtime_lib_api.js.html

/**
     * Post a typed instance to a HTTP URL
     * @method module:composer-runtime#post
     * @param {string} url The URL to post the data to
     * @param {Typed} typed The typed instance to be posted. The instance will be serialized to JSON.
     * @param {object} options The options that are passed to Serializer.toJSON
     * @return {Promise} A promise. The promise is resolved with a HttpResponse
     * that represents the result of the HTTP POST.
     * @public
     */
    this.post = function post(url, typed, options) {
        const method = 'post';
        LOG.entry(method, url, typed);
        const data = serializer.toJSON(typed, options);
        LOG.debug(method, typed.getFullyQualifiedType(), data);

        return httpService.post(url, data)
            .then((response) => {
                LOG.exit(method);
                return Promise.resolve(response);
            });
    };

但我可以看到任何方式..

我的代码:

    /**
 * Transaction to allow parties to Monitor planes
 * @param {org.****.MonitorPlane} monitorPlane
 * @transaction
 */
function monitorPlane(monitorPlane){
    var NS = 'org.****';
    plane = monitorPlane.plane
    var location
    return location = getLocation()
    .then(function(){
        plane.lat = lat
        plane.long = long
        if(plane.monitorPlane){
            plane.monitorPlane.push(monitorPlane)
        }else{
            plane.monitorPlane = [monitorPlane]
        }
    }).then(function(){
        return getAssetRegistry(NS + '.Plane')
    }).then(function(planeRegistry){
        return planeRegistry.update(plane);
    });


function getLocation(){

    var url = 'https://opensky-network.org/api/states/all?time=1458564121&icao24=3c6444';
    var data = {};

    return post(url,data)
      .then(function (resp) {
          console.log(resp);
          return resp;
    })
    .then(function(data) {
        console.log(data);

        lat = data.states[0][5]
        long = data.states[0][6]
        lat = lat.toString()
        long = long.toString()
        location = [lat,long]
        return location
      })
      .catch((err) => {
          console.log(err);
      });
    }
}

是否可以从事务处理器功能执行此操作?

hyperledger-fabric hyperledger hyperledger-composer
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.