'how to generate Custom Sequential Id like 0001 and $http.post() in angularJS with couchDB after getting it from $http.get(/View)

I am working on a project which I should generate a Custom sequential Id ex: 00001 using angularJS with CouchDB after every submit button from the modal. I already did generate it. however, I encounter some issues. It seems that whenever I use $http.post() It always duplicate the insert entries to the couchDB view.

Here is my code.

$scope.getUID = function() {
    $http.get($scope.hostUrl + '/_design/module4/_view/byGetUID').then(function(response){
      var ret = response.data.rows;
      ret.forEach(function(itm){
        var dt = itm.value;
        

        //for Acronym or prefix
        if(dt.payment_mode == "Cash"){
          dt.prepayment_code = "CSH";
        }else if(dt.payment_mode == "Telegraphic Transfer"){
          dt.prepayment_code = "TLT";
        }else if(dt.payment_mode == "Cheque"){
          dt.prepayment_code = "CHQ";
        }else if(dt.payment_mode == "Change"){
          dt.prepayment_code = "CHG";}
        


        $scope.tmpTotal_Uid += itm.value.count_UID;
        if($scope.tmpUid_Count <= $scope.tmpTotal_Uid){
          $scope.tmpUid_Count++;

          $scope.tmp_dt_uid = (dt.prepayment_code+'0000000' +$scope.tmpUid_Count).slice(-7);
          dt.total_transaction_count = $scope.tmpTotal_Uid;
          dt.prepaymentUID = $scope.tmp_dt_uid;

        }else {
          console.log("same Value Already!");
        }
        
        console.log("total:",$scope.tmpTotal_Uid);
        console.log("tmpUid_Count",$scope.tmpUid_Count);
        console.log("tmp_dt_uid",$scope.tmp_dt_uid);
        console.log("dt_total_transaction_count",dt.total_transaction_count);
        console.log("dt.prepaymentUID",dt.prepaymentUID);
        console.log("payment_mode",dt.prepayment_code,"prepayment_code",dt.prepayment_code);
        console.log(typeof itm.value.count_UID);
        //arrayList Below
        
        $scope.datasList.push(dt);
        console.log("datasList",$scope.datasList);

        //Encounter Error nag duduplicate 7 rows then 8 entries 7*8 = 56 upon checking.
       

        let new_credit_wallet = {
          "customer_company_id":dt._id,
          "credit_amount":dt.credit_amount,
          "math_operation":dt.math_operation,
          "created_at": Date.now(),
          "message":dt.message,
          "bank_name": dt.bank_name,
          "payment_mode": dt.payment_mode,
          "date_deposit": dt.date_deposit,
          "total_transaction_count":dt.total_transaction_count,
          "prepayment_status":dt.prepayment_status,
          "cash_date_receive":dt.cash_date_receive,
          "count_UID":dt.count_UID,
          "prepaymentUID":dt.prepaymentUID,
          "prepayment_code":dt.prepayment_code,
          "type": "CreditWallet2021" 
        }

        $http.post($scope.hostUrl, new_credit_wallet).then(function(response){
          var resp = response.data;
          console.log("resp success",resp);
          console.log("credit_wallet", new_credit_wallet);
        }).then(function(){
         
        })

      })
      
    }).then(function(){
    });
   }
   
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.0/angular.min.js"></script>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source