Hyperledger fabric .update和.updatell未按需要运行

Hyperledger fabric .update和.updatell未按需要运行,hyperledger-fabric,hyperledger,hyperledger-composer,Hyperledger Fabric,Hyperledger,Hyperledger Composer,我正在编写一个关于Composer的应用程序,它与操场上的示例Bond network非常相似。我的申请中有一部分要求在到期日删除过期的合同。因此,代码必须获取所有合同,找到日期小于当前日期的合同,并将其有效字段更改为假。下面是我运行的一些代码: function SOD(sod) { // eslint-disable-line no-unused-vars return getAssetRegistry('org.acme.otc.LiveContract') .then(functio

我正在编写一个关于Composer的应用程序,它与操场上的示例Bond network非常相似。我的申请中有一部分要求在到期日删除过期的合同。因此,代码必须获取所有合同,找到日期小于当前日期的合同,并将其
有效
字段更改为
。下面是我运行的一些代码:

function SOD(sod) {  // eslint-disable-line no-unused-vars

return getAssetRegistry('org.acme.otc.LiveContract')
.then(function (contractRegistry){

    return contractRegistry.getAll();
})
.then(function (l){
    allContracts=l;
    //console.log('Curr List: ',allContracts);
    var q = new Date();
    var m = q.getMonth();
    var d = q.getDay();
    var y = q.getFullYear();
    var date = new Date(y,m,d);
    var arr=[];
    allContracts.forEach(function(contract) {
      //console.log('Contract Date: ',contract.contract.expiry);
      if (contract.contract.expiry<date) {
        //console.log('Checks Out');
        //console.log('Destroying: ',contract);

        arr.push(contract);
        //console.log('Return: ',x);
      } 
    }) 
return arr
}).then( function(arr){
    var factory = getFactory();
    new_arr=[];

    fLen = arr.length;
    for (i = 0; i < fLen; i++) {
        var old_contract=arr[i];
        console.log('ISIN: ',old_contract.ISINcode);
        var new_contract = factory.newResource('org.acme.otc', 'LiveContract', old_contract.ISINcode);
        console.log('Entry: ',old_contract);
        new_contract.contract=old_contract.contract;
        new_contract.valid=false;
        new_arr.push(new_contract);


    } x=invalidate(new_arr);
});
//权限.acl

  rule Issuer {
      description: "Allow full access to the issuer of a bond"
      participant(i): "org.acme.otc.Trader"
      operation: ALL
      resource(a): "org.acme.otc.OpenOrder"
      condition: (a.bond.issuer.memberId === i.memberId)
      action: ALLOW
  }

  rule Default {
      description: "Allow read access"
      participant: "org.acme.otc.*"
      operation: ALL
      resource: "org.acme.otc.*"
      action: ALLOW
  }

  rule SystemACL {
      description:  "System ACL to permit all access"
      participant: "org.hyperledger.composer.system.Participant"
      operation: ALL
      resource: "org.hyperledger.composer.system.**"
      action: ALLOW
  }

  rule NetworkAdminUser {
      description: "Grant business network administrators full access to user resources"
      participant: "org.hyperledger.composer.system.NetworkAdmin"
      operation: ALL
      resource: "**"
      action: ALLOW
  }

  rule NetworkAdminSystem {
      description: "Grant business network administrators full access to system resources"
      participant: "org.hyperledger.composer.system.NetworkAdmin"
      operation: ALL
      resource: "org.hyperledger.composer.system.**"
      action: ALLOW
  }
我对JavaScript非常缺乏经验,因此错误可能很小。谢谢你的帮助

更新: 我正在研究保罗的回答。我的新代码如下

/**
 * Start of Day Update
 * @param {org.acme.otc.SOD} sod - the sod transaction
 * @transaction
 */
function SOD(sod) {  // eslint-disable-line no-unused-vars
  //arr=pare();
  var aR=null;
  return getAssetRegistry('org.acme.otc.LiveContract')
    .then(function (contractRegistry){
        ar=contractRegistry;
        return ar.getAll();
  })
    .then(function (l){
        allContracts=[];
        allContracts=l;
        console.log('Curr List: ',allContracts);
        var q = new Date();
        var m = q.getMonth();
        var d = q.getDay();
        var y = q.getFullYear();
        var date = new Date(y,m,d);
        console.log('Current Date: ',date);
        arr=[];
        allContracts.forEach(function(contract) {
          //console.log('Contract Date: ',contract.contract.expiry);
          if (contract.contract.expiry<date) {
            //console.log('Checks Out');
            //console.log('Destroying: ',contract);

            arr.push(contract);
            //console.log('Return: ',x);
          } 
        }) 
    return arr
    }).then( function(arr){
        var factory = getFactory();
        new_arr=[];

        fLen = arr.length;
        for (i = 0; i < fLen; i++) {
            var old_contract=arr[i];
            console.log('ISIN: ',old_contract.ISINcode);
            var new_contract = factory.newResource('org.acme.otc', 'LiveContract', old_contract.ISINcode);
            console.log('Entry: ',old_contract);
            new_contract.contract=old_contract.contract;
            new_contract.valid=false;
            new_arr.push(new_contract);


        } 
    console.log(new_arr);
  });
  console.log("getting to update the records");
  return  aR.updateAll(new_arr);

}
/**
*每日开始更新
*@param{org.acme.otc.SOD}SOD-SOD事务
*@交易
*/
函数SOD(SOD){//eslint禁用行无未使用的变量
//arr=pare();
var-aR=null;
返回getAssetRegistry('org.acme.otc.LiveContract')
.then(功能(合同注册){
ar=合同登记处;
返回ar.getAll();
})
.然后(函数(l){
所有合同=[];
所有合同=l;
log('Curr List:',allContracts);
var q=新日期();
var m=q.getMonth();
var d=q.getDay();
变量y=q.getFullYear();
var日期=新日期(y、m、d);
console.log('当前日期:',日期);
arr=[];
allContracts.forEach(功能(合同){
//console.log('Contract Date:',Contract.Contract.expiration);

如果(contract.contract.expiry没有看到
updateAll
代码位,或者不知道
invalidate(new\u arr)
有什么功能,那么我会发布一些类似的东西,但你得到了要点-它在操场上起作用。
updatell
updateAll
对我来说都很好

我已经在下面粘贴了工作代码——只是从您的名称空间等给了它一个事务/TP装饰器

更新: (我的原始帖子我将留在这里-因为它起作用了)-我还更新了第二个新代码块的工作版本-但有一些更正,例如,您没有正确引用对象(例如
ar
,而不是
ar
,因此它无法更新注册表等)而且-你需要遵循JS承诺链,使用你已经做到的方式-因此我在下面的“答案2”中为你添加了代码。)此外,可能最好声明
new\u arr
具有全局功能范围,我已经在代码中做到了这一点

/**
 * New script file
 */

/**
* A transaction processor function description
* @param { org.acme.otc.SOD } sod A human description of the parameter
* @transaction
*/

function SOD(sod) {  // eslint-disable-line no-unused-vars

  var aR = null;

  return getAssetRegistry('org.acme.otc.LiveContract')
  .then(function (contractRegistry){
     // return contractRegistry.getAll();
      aR = contractRegistry;
      return aR.getAll();
  })
  .then(function (l) {

      allContracts=[];
      allContracts=l;

      console.log('allContracts List is: ' + allContracts );

      var q = new Date();
      var m = q.getMonth();
      var d = q.getDay();
      var y = q.getFullYear();
      var datevar = new Date(y,m,d);
      arr=[];

      allContracts.forEach(function(contract) {
      console.log('Contract Date: ' + contract.contract.expiry);

      if (contract.contract.expiry < datevar ) {
      console.log('Checks Out - this contract needs fixing');
      console.log('Destroying: ' + contract);

      arr.push(contract);
      //console.log('Return: ',x);
      } 
    }) 
  return arr
  })
  .then( function(arr) {
      var factory = getFactory();
      new_arr=[];

      fLen = arr.length;
      for (i = 0; i < fLen; i++) {
          var old_contract=arr[i];
          console.log('ISIN: ' + old_contract.ISINcode);
          var new_contract = factory.newResource('org.acme.otc', 'LiveContract', old_contract.ISINcode);
          console.log('Entry: ' + old_contract);
          new_contract.contract=old_contract.contract;
          new_contract.valid=false;
          new_arr.push(new_contract);


       }  // x=invalidate(new_arr);


   });

   console.log("getting to update the records");
   return  aR.updateAll(new_arr);
    //.then(function () {

       // return getAssetRegistry('org.acme.otc.LiveContract')
       //.then(function (contractRegistry){

       //  return contractRegistry.updateAll(new_arr);

    // });
   //});
}
/**
*新脚本文件
*/
/**
*事务处理器功能描述
*@param{org.acme.otc.SOD}SOD参数的人类描述
*@交易
*/
函数SOD(SOD){//eslint禁用行无未使用的变量
var-aR=null;
返回getAssetRegistry('org.acme.otc.LiveContract')
.then(功能(合同注册){
//返回contractRegistry.getAll();
aR=合同登记处;
返回aR.getAll();
})
.然后(函数(l){
所有合同=[];
所有合同=l;
log('所有合同列表为:'+所有合同);
var q=新日期();
var m=q.getMonth();
var d=q.getDay();
变量y=q.getFullYear();
var datevar=新日期(y、m、d);
arr=[];
allContracts.forEach(功能(合同){
console.log('合同日期:'+合同.合同.到期日);
if(contract.contract.expiration
回答2:使用第二个代码块更新代码

/**
 * Start of Day Update
 * @param {org.acme.otc.SOD} sod - the sod transaction
 * @transaction
 */
function SOD(sod) {  // eslint-disable-line no-unused-vars
  //arr=pare();

  var aR=null;
  new_arr=[]; 

  return getAssetRegistry('org.acme.otc.LiveContract')
    .then(function (contractRegistry){
        aR=contractRegistry;
        return aR.getAll();

  })
    .then(function (l){
        allContracts=[];
        allContracts=l;
        console.log('Curr List: ',allContracts);
        var q = new Date();
        var m = q.getMonth();
        var d = q.getDay();
        var y = q.getFullYear();
        var date = new Date(y,m,d);
        console.log('Current Date: ',date);
        arr=[];
        allContracts.forEach(function(contract) {
          //console.log('Contract Date: ',contract.contract.expiry);
          if (contract.contract.expiry < date) {
            //console.log('Checks Out');
            //console.log('Destroying: ',contract);

            arr.push(contract);
          //console.log('Return: ',x);
          } 
        }) 
    return arr
    }).then( function(arr){
        var factory = getFactory();

        //new_arr=[];

        fLen = arr.length;
        for (i = 0; i < fLen; i++) {
            var old_contract=arr[i];
            console.log('ISIN: ',old_contract.ISINcode);
            var new_contract = factory.newResource('org.acme.otc', 'LiveContract', old_contract.ISINcode);
            console.log('Entry: ',old_contract);
            new_contract.contract=old_contract.contract;
            new_contract.valid=false;
            new_arr.push(new_contract);


        } 
       console.log("New Array is now " + new_arr);
  })
   .then(function () {

      console.log("getting to update the records");
      return  aR.updateAll(new_arr);
   });
}
/**
*每日开始更新
*@param{org.acme.otc.SOD}SOD-SOD事务
*@交易
*/
函数SOD(SOD){//eslint禁用行无未使用的变量
//arr=pare();
var-aR=null;
新_arr=[];
返回getAssetRegistry('org.acme.otc.LiveContract')
.then(功能(合同注册){
aR=合同登记处;
返回aR.getAll();
})
.然后(函数(l){
所有合同=[];
所有合同=l;
log('Curr List:',allContracts);
var q=新日期();
var m=q.getMonth();
var d=q.getDay();
变量y=q.getFullYear();
var日期=新日期(y、m、d);
console.log('当前日期:',日期);
arr=[];
allContracts.forEach(功能(合同){
//console.log('Contract Date:',Contract.Contract.expiration);
如果(合同期限<日期){
//log('checkout');
//console.log('销毁:',合同);
arr.push(合同);
//log('Return:',x);
} 
}) 
返回arr
}).然后(功能(arr){
var factory=getFactory();
//新_arr=[];
fLen=阵列长度;
对于(i=0;i/**
 * Start of Day Update
 * @param {org.acme.otc.SOD} sod - the sod transaction
 * @transaction
 */
function SOD(sod) {  // eslint-disable-line no-unused-vars
  //arr=pare();

  var aR=null;
  new_arr=[]; 

  return getAssetRegistry('org.acme.otc.LiveContract')
    .then(function (contractRegistry){
        aR=contractRegistry;
        return aR.getAll();

  })
    .then(function (l){
        allContracts=[];
        allContracts=l;
        console.log('Curr List: ',allContracts);
        var q = new Date();
        var m = q.getMonth();
        var d = q.getDay();
        var y = q.getFullYear();
        var date = new Date(y,m,d);
        console.log('Current Date: ',date);
        arr=[];
        allContracts.forEach(function(contract) {
          //console.log('Contract Date: ',contract.contract.expiry);
          if (contract.contract.expiry < date) {
            //console.log('Checks Out');
            //console.log('Destroying: ',contract);

            arr.push(contract);
          //console.log('Return: ',x);
          } 
        }) 
    return arr
    }).then( function(arr){
        var factory = getFactory();

        //new_arr=[];

        fLen = arr.length;
        for (i = 0; i < fLen; i++) {
            var old_contract=arr[i];
            console.log('ISIN: ',old_contract.ISINcode);
            var new_contract = factory.newResource('org.acme.otc', 'LiveContract', old_contract.ISINcode);
            console.log('Entry: ',old_contract);
            new_contract.contract=old_contract.contract;
            new_contract.valid=false;
            new_arr.push(new_contract);


        } 
       console.log("New Array is now " + new_arr);
  })
   .then(function () {

      console.log("getting to update the records");
      return  aR.updateAll(new_arr);
   });
}