HyperLedger Composer:Tranaction错误TypeError:getAssetRegistry(...)。get不是函数

时间:2017-09-28 05:22:42

标签: javascript hyperledger-composer

我试图在Composer中重新创建一个用GoLang编写的链代码。 的 Model.cto

asset Carton identified by cartonId {
o String cartonId
o String manufacturerId
o String dateOfDeparture optional
o String recipient optional 
o String currentOwner
o String status
--> Unit[] units optional
o Trackrecord[] trackrecord optional 
}
 transaction MakeCarton {
 --> Unit[] unit
 o String Id
 }
 asset Unit identified by unitId {
 o String unitId
 o String cartonId
 o String manufacturerId
 o String dateOfDeparture
 o String recipient
 o Trackrecord[] trackrecord
 o String currentOwner
 o String status  
  }

所以我需要创建一个创建一个纸箱并接受单位数组和cartonId的交易。每个纸箱都包含一些单位,单位也是资产,所以我需要在世界各地的单位资产中更新他们的纸箱。

  function makeCarton(make) {
   var carton = getFactory().newResource('org.acme.mynetwork','Carton',make.Id);
   //carton.cartonId = make.Id ;
    var unitobjs = new Array() ;
    for(var i=0; i < make.unit.length ; i++) {
    var unitobj = getFactory().newResource('org.acme.mynetwork','Unit',make.unit[i].unitId) ;
    unitobj.cartonId = make.Id ;     
    unitobj = getFactory().newRelationship('org.acme.mynetwork','Unit',make.unit[i].unitId) ;
    unitobjs.push(unitobj) ;
     }

     carton.units = unitobjs ;

    // getFactory().newRelationship('org.acme.mynetwork','Unit',make.unit[i].unitId)
    for(var i=0; i < make.unit.length ; i++) {  
    carton.manufacturerId= make.unit[i].manufacturerId ;
     carton.currentOwner = make.unit[i].currentOwner ;
     carton.status = 'At '+ make.unit[i].currentOwner ;
      }

      return getAssetRegistry('org.acme.mynetwork.Carton').then(function (assetRegistry) {
     // Update the asset in the asset registry.
      return assetRegistry.add(carton).then(function() {
      for(var i=0; i < make.unit.length ; i++) {
       return getAssetRegistry('org.acme.mynetwork.Unit').then(function (unitRegistry) {

         // var unitobj = getFactory().newResource('org.acme.mynetwork','Unit',make.unit[i].unitId) ;
           var unitobj = getAssetRegistry('org.acme.mynetwork.Unit').get(make.unit[i].unitId);
           //unitRegistry.get(make.unit[i].unitId);
     unitobj.cartonId = make.Id ;      
      return unitRegistry.update(unitobj);
    })
      //.then(function(unitobj){
    //unitobj.cartonId = make.Id;
    //untiRegistry.update(unitobj);});
     }
});
});
 }

提交此事务会生成 TypeError:getAssetRegistry(...)。get不是函数

1 个答案:

答案 0 :(得分:0)

var unitobj = getAssetRegistry('org.acme.mynetwork.Unit').get(make.unit[i].unitId); 会将Promise返回给资产注册表,因此您需要.then,就像您对getAssetRegistry()的其他调用一样。

这可能会有所帮助: https://github.com/hyperledger/composer-sample-networks/blob/master/packages/perishable-network/lib/logic.js#L89